【发布时间】:2014-09-02 13:35:01
【问题描述】:
我将 HTTPS 添加到 node.js 应用程序,同时将 HTTP 转发到 HTTPS。我正在使用以下代码:
// Trusting proxy
self.app.enable('trust proxy');
// Http -> Https redirection middleware
self.app.use(function (req, res, next) {
var schema = req.protocol.toLowerCase();
console.log("Redirection check, schema: " + schema);
if (schema === 'https') {
console.log("Next");
next();
} else {
var tmp = 'https://' + req.headers.host + req.url;
console.log("Redirect to: " + tmp);
res.redirect(tmp);
}
});
当我浏览 https://localhost:8443/index.html 时一切正常,页面打开正常。
但是当我尝试http://localhost:8443/index.html 时,Chrome 似乎将 url 重定向或重写为localhost:8443/index.html,并且 Chrome 永远等待 localhost。我的应用程序没有控制台消息。我在 Windows 7 下使用 Chrome。
我的代码有什么问题?
【问题讨论】:
标签: javascript node.js redirect https