Created
October 17, 2019 23:27
-
-
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)
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
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