2016-12-06 81 views
0

我已將我的node.js應用程序移動到https。我通過nginx反向代理設置了https。我有一個問題,但是當我輸入example.com時,它不會重定向到https://example.com。所以我嘗試寫我的app.js將http重定向到https,除了在本地主機上

app.get('*', function(req, res, next) { 
    if (req.headers.host.indexOf('localhost') > -1 || req.secure) { 
    return next(); 
    }else{ 
    res.redirect('https://'+req.hostname+req.url); 
    } 
}); 

它不工作時,我重新部署,任何線索?

+0

錯誤日誌?他們傾向於幫助... – Flummox

回答

0

,如果你做應用Express模塊​​和你想用「/」響應,那麼簡單的情況下,檢查以下條款:

app.get('/', function(req, res, next) { 
    if (req.headers.host.indexOf('localhost') > -1 || req.secure) { 
    return next(); 
    }else{ 
    res.redirect('https://google.com'); 
    } 
}); 

它檢查你的代碼。如果你要聽我的話安全服務器createServer與exress與安全的方式像

var secureServer = require('https').createServer(httpsOptions, app); 

httpsOptions數據它是完全然後運行的證書文件和密鑰文件

相關問題