koa-enforces-ssl
Version:
Enforces SSL for Node.js Koa projects
40 lines (24 loc) • 1.1 kB
Markdown
[](https://travis-ci.org/rvanmil/koa-enforces-ssl)
# koa-enforces-ssl
This simple module enforces HTTPS connections on any incoming requests. In case of a non-encrypted HTTP request, koa-enforces-ssl automatically redirects to an HTTPS address using a 301 permanent redirect.
koa-enforces-ssl also works behind reverse proxies (load balancers) as they are for example used by Heroku and nodejitsu. In such cases, however, the `proxy` parameter has to be set to `true` (see below).
## Usage
```bash
npm install koa-enforces-ssl --save
```
```javascript
const Koa = require('koa')
const ssl = require('koa-enforces-ssl')
const app = new Koa()
app.proxy = true
app.use(ssl)
app.use(async ctx => {
ctx.body = 'Hello World'
})
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`)
})
```
## Prior art
This module was heavily inspired by [Hengki Sihombing's express-enforces-ssl module](https://github.com/hengkiardo/express-enforces-ssl).