Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Created October 17, 2019 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatrickAlphaC/680c5a43b16b0f21fd3486caa1c36ea1 to your computer and use it in GitHub Desktop.
Save PatrickAlphaC/680c5a43b16b0f21fd3486caa1c36ea1 to your computer and use it in GitHub Desktop.
A chunk of code to add to your app.js file in node to make it rediect http to https (not-secure to secure)
var app = express()
// Make sure you call this before you call any app.get functions.
app.use(requireHTTPS);
// code here, with gets and such
function requireHTTPS(req, res, next) {
// The 'x-forwarded-proto' check is for Heroku
if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
return res.redirect('https://' + req.get('host') + req.url);
}
next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment