admin管理员组文章数量:1417426
I don't understand why my following code does not acplish this? Can someone explain where I am going wrong? All http requests should be redirected to https on heroku, but not on localhost. If someone could point me to an example of this working I would really appreciate it. I feel like this should be very simple and 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 some searching already and it looks like what I have should work.
I don't understand why my following code does not acplish this? Can someone explain where I am going wrong? All http requests should be redirected to https on heroku, but not on localhost. If someone could point me to an example of this working I would really appreciate it. I feel like this should be very simple and 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 some searching already and it looks like what I have should work.
Share Improve this question asked Apr 22, 2015 at 23:12 mikestaubmikestaub 2,1834 gold badges27 silver badges34 bronze badges2 Answers
Reset to default 7Your middleware's req, res, next
params are being lost by having been wrapped by an 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);
The open source express-force-ssl library checks X-Forwarded-Proto
and ought to work with Heroku. The code is very simple.
本文标签: javascriptHow to force https redirect on heroku with express 40Stack Overflow
版权声明:本文标题:javascript - How to force https redirect on heroku with express 4.0? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745261363a2650370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论