Skip to content

Instantly share code, notes, and snippets.

@gingi
Last active April 22, 2016 03:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gingi/47df42a45bb1653002ee to your computer and use it in GitHub Desktop.
Save gingi/47df42a45bb1653002ee to your computer and use it in GitHub Desktop.
Redirecting HTTP requests to HTTPS in a Node.js Connect app.
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