node.js 将 expressjs 绑定到特定的 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9986220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind expressjs to a specific IP address
提问by ReiGarcia
How can i bind a expressjs server to a specific IP
如何将 expressjs 服务器绑定到特定 IP
Something like
就像是
app.listen(8888, '192.168.0.101');
Equivalent to nodejs:
相当于nodejs:
http.createServer(onRequest).listen(8888,'192.168.0.101');
回答by Jacob Krall
ExpressJS just passes your parameters downto the httpmodule when you call listen, so your example should work.
当您调用 listen 时,ExpressJS 只是将您的参数传递给http模块,因此您的示例应该可以工作。
Is that not the case?
不是这样吗?
回答by Rubin bhandari
var server = app.listen(3000, '127.0.0.1',onServerListening);
In this case I want the server to respond only to connections using the 127.0.0.1 host name. Not 0.0.0.0, and not localhost. Only 127.0.0.1.
在这种情况下,我希望服务器仅响应使用 127.0.0.1 主机名的连接。不是 0.0.0.0,也不是本地主机。只有 127.0.0.1。
回答by chovy
app.set('domain', 'myhost.whatever');
app.set('port', process.env.PORT || 8080);

