Javascript 节点应用程序:Http到Https重定向不适用于localhost
我将HTTPS添加到Javascript 节点应用程序:Http到Https重定向不适用于localhost,javascript,node.js,redirect,https,Javascript,Node.js,Redirect,Https,我将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();
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。没有来自我的应用程序的控制台消息。我在Windows7下使用Chrome
我的代码怎么了?Chrome只是隐藏了URL的
http://
部分,这是正常的
您不能在同一端口上运行http
和https
您已在地址中指定了
http
,因此浏览器尝试与http连接。服务器正在侦听HTTPS,因此失败。有意义。我忘记了在Openshift下,你只需要听一个端口。当然,我需要在笔记本电脑上安装第二台服务器。现在就试试。