Last active
April 22, 2016 03:10
-
-
Save gingi/47df42a45bb1653002ee to your computer and use it in GitHub Desktop.
Redirecting HTTP requests to HTTPS in a Node.js Connect app.
This file contains 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
function requireHTTPS(req, res, next) { | |
if (!req.secure) { | |
//FYI this should work for local development as well | |
var domain = "https://" + req.get("host"); | |
if (process.env["SSL_PORT"]) { | |
domain = domain.replace(/:\d+$/, ""); | |
domain += ":" + process.env["SSL_PORT"]; | |
} | |
return res.redirect(domain + req.url); | |
} | |
next(); | |
} | |
app.use(requireHTTPS); | |
app.get('/', routeHandlerHome); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment