2014-09-02 28 views
1

我正在將HTTPS添加到node.js應用程序,同時將HTTP轉發到HTTPS。我使用下面的代碼位:節點應用程序: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始終等待本地主機。我的應用程序沒有控制檯消息。我在Windows 7下使用Chrome。

我的代碼有什麼問題?

回答

2

Chrome只是隱藏了網址的http://部分,這很正常。

您不能在同一個端口上運行httphttps

您在地址中指定了http,因此瀏覽器嘗試連接HTTP。服務器正在偵聽HTTPS,因此失敗。

+0

有道理。我忘記了在Openshift下,你只需要聽一個端口。但是,當然,我需要在筆記本電腦上安裝第二臺服務器。現在嘗試。 – JVerstry 2014-09-02 13:40:47

相關問題