2017-10-13 149 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); 
相关问题