我不明白爲什麼我的下面的代碼沒有完成這個?有人能解釋我要去哪裏嗎?所有的http請求都應該在heroku上重定向到https,而不是在本地主機上。如果有人能指出我這個工作的例子,我會非常感激。我覺得這應該是非常簡單和直接的。如何使用express 4.0強制對heroku進行https重定向?
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;
我已經做了一些搜索,它看起來像我應該工作。
這是解決方案,謝謝! – mikestaub
非常簡單,謝謝!並不是說它會顯着影響性能,但在我看來,將if語句放在整個代碼段而不是添加生產之外的noop中間件會更有意義。 – stone
這適用於我,但重定向到端口80,這不起作用。我怎樣才能重定向到443? –