2017-10-13 91 views
1

我想在NodeJS-app中同時提供HTTP和HTTPS。它適用於內部應用程序,其中一些訪問者不支持HTTPS。在NodeJS中偵聽HTTP和HTTPS的正確方法

這是簡單的正確方法,還是應該是2個獨立的NodeJS應用程序?

http.createServer(app).listen(80, function() { 
    console.log('My insecure site'); 
}); 

https.createServer(options, app).listen(443, function() { 
    console.log('My sdecure site'); 
}); 
+0

這是正確的方法。 – TGrif

回答

1

我不認爲有更好的辦法。你可以做更多的優化,因爲HTTP和HTTPS服務器都做同樣的事情。創建一個名爲register的函數,該函數將配置中間件和路由。然後只需爲HTTP和HTTPS調用它。

var register = function (app) { 
    // config middleware 
    app.configure({ 

    }); 

    // config routes 
    app.get(...); 
}; 

var http = express.createServer(); 
register(http); 
http.listen(80); 

var https = express.createServer(); 
register(https); 
https.listen(443); 
相關問題