Created
November 1, 2016 10:51
-
-
Save benhowdle89/31ad39292241e6ba91ee7b937c9292d5 to your computer and use it in GitHub Desktop.
Redirect non-HTTPS -> HTTPS in your Express app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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