Skip to content

Instantly share code, notes, and snippets.

@benhowdle89
Created November 1, 2016 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhowdle89/31ad39292241e6ba91ee7b937c9292d5 to your computer and use it in GitHub Desktop.
Save benhowdle89/31ad39292241e6ba91ee7b937c9292d5 to your computer and use it in GitHub Desktop.
Redirect non-HTTPS -> HTTPS in your Express app
const requireHTTPS = (req, res, next) => {
if(req.headers['x-forwarded-proto'] !== 'https' && process.env.NODE_ENV === 'production') {
var secureUrl = "https://" + req.headers['host'] + req.url
res.writeHead(301, { "Location": secureUrl })
res.end()
}
next()
}
app.use(requireHTTPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment