如何让 Node.JS Express 只在本地主机上监听?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10068714/
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
How to get Node.JS Express to listen only on localhost?
提问by nak
I have an application that I have behind a reverse proxy, I would like for it to only listen to localhost/127.0.0.1.
我有一个使用反向代理的应用程序,我希望它只侦听 localhost/127.0.0.1。
I expected this to work:
我希望这能奏效:
app.listen(3001, 'localhost');
app.listen(3001, 'localhost');
or
或者
app.listen(3001, '127.0.0.1');
app.listen(3001, '127.0.0.1');
...but instead I get an error:
...但我收到一个错误:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot read property 'port' of null
at Object.<anonymous> (/home/ctoledo/hive-go/go.js:204:76)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)
Running the application without a specifying the hostname works fine, ie., app.listen(3001);.
在不指定主机名的情况下运行应用程序可以正常工作,即app.listen(3001);.
I am running Node v0.6.14and express@2.5.5and have read this google groups discussionand have found this commentin Express application.js saying: "This method takes the same arguments as node's http.Server#listen()."
我正在运行 Node v0.6.14和 express@ 2.5.5并阅读了这个谷歌小组讨论,并在 Express application.js 中发现了这条评论说:“此方法采用与 node 相同的参数http.Server#listen()。”
Thanks for any help.
谢谢你的帮助。
回答by loganfsmyth
Thanks for the info, think I see the problem. This is a bug in hive-gothat only shows up when you add a host. The last lines of it are:
感谢您的信息,我想我看到了问题。这是一个错误hive-go,只有在您添加主机时才会出现。它的最后几行是:
app.listen(3001);
console.log("... port %d in %s mode", app.address().port, app.settings.env);
When you add the host on the first line, it is crashing when it calls app.address().port.
当您在第一行添加主机时,它在调用app.address().port.
The problem is the potentially asynchronous nature of .listen(). Really it should be doing that console.logcall inside a callback passed to listen. When you add the host, it tries to do a DNS lookup, which is async. So when that line tries to fetch the address, there isn't one yet because the DNS request is running, so it crashes.
问题在于.listen(). 实际上,它应该在console.log传递给侦听的回调中执行该调用。添加主机时,它会尝试执行异步的 DNS 查找。因此,当该行尝试获取地址时,还没有地址,因为 DNS 请求正在运行,因此崩溃了。
Try this:
尝试这个:
app.listen(3001, 'localhost', function() {
console.log("... port %d in %s mode", app.address().port, app.settings.env);
});
回答by dgh
You are having this problem because you are attempting to console log app.address() before the connection has been made. You just have to be sure to console log after the connection is made, i.e. in a callback or after an event signaling that the connection has been made.
您遇到此问题是因为您试图在建立连接之前控制台 log app.address()。您只需要确保在建立连接后控制台日志,即在回调中或在表明连接已建立的事件之后。
Fortunately, the 'listening' event is emitted by the server after the connection is made so just do this:
幸运的是,'listening' 事件是在建立连接后由服务器发出的,所以只需执行以下操作:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
app.get('/', function(req, res) {
res.send("Hello World!");
});
server.listen(3000, 'localhost');
server.on('listening', function() {
console.log('Express server started on port %s at %s', server.address().port, server.address().address);
});
This works just fine in nodejs v0.6+ and Express v3.0+.
这在 nodejs v0.6+ 和 Express v3.0+ 中工作得很好。

