节点应用程序:Http到Https重定向不适用于localhost

时间:2014-09-02 13:35:01

标签: 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();
        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似乎会将网址重定向或重写为localhost:8443/index.html,Chrome会永远等待localhost。我的应用程序没有控制台消息。我在Chrome 7的Windows 7下。

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:2)

Chrome只是隐藏了网址的http://部分,这是正常的。

您无法在同一端口上运行httphttps

您已在地址中指定了http,因此浏览器会尝试使用HTTP进行连接。服务器正在侦听HTTPS,因此失败。

相关问题