javascript - How to force https redirect on heroku with express 4.0? -
i don't understand why following code not accomplish this? can explain going wrong? http requests should redirected https on heroku, not on localhost. if point me example of working appreciate it. feel should simple , straightforward.
var app = express(); var https_redirect = function () { return function(req, res, next) { if(process.env.node_env === 'production'){ if(req.headers['x-forwarded-proto'] != 'https') { return res.redirect('https://' + req.headers.host + req.url); } else { return next(); } } else { return next(); } }; }; app.use(https_redirect()); var server = app.listen(config.port, config.ip, function () { }); exports = module.exports = app;
i did searching , looks have should work.
your middleware's req, res, next
params being lost having been wrapped outer function.
try this:
var https_redirect = function(req, res, next) { if (process.env.node_env === 'production') { if (req.headers['x-forwarded-proto'] != 'https') { return res.redirect('https://' + req.headers.host + req.url); } else { return next(); } } else { return next(); } }; app.use(https_redirect);