我想强制某些路线始终在我的 Express 应用程序中使用安全连接。我如何检查以确保它使用 https?
我在 heroku 上使用搭载 ssl 进行部署。
我也在 Heroku 上部署。当他们使用 nginx 进行反向代理时,他们添加了一堆标头。在这种情况下,我们感兴趣的是 x-forwarded-proto。
这就是我所做的:
app.get(/\/register$/, function(req, res){
console.log(JSON.stringify(req.headers)); //to see all headers that heroku adds
if(req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'] === "http") {
res.redirect("https://" + req.headers.host + req.url);
}
else {
//the rest of your logic to handle this route
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)