koa-enforces-ssl
Version: 
Enforces SSL for Node.js Koa projects
17 lines (15 loc) • 346 B
JavaScript
module.exports = (ctx, next) => {
  if (ctx.secure) {
    return next()
  } else {
    redirect(ctx)
  }
}
const redirect = (ctx) => {
  if (ctx.method === 'GET') {
    ctx.status = 301
    ctx.redirect(`https://${ctx.headers.host}${ctx.originalUrl}`)
  } else {
    ctx.throw(403, 'Please use HTTPS when submitting data to this server.')
  }
}