Javascript 在 Node.js 中收听 EADDRNOTAVAIL 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10809740/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:02:23  来源:igfitidea点击:

listen EADDRNOTAVAIL error in Node.js

javascriptnode.js

提问by thuan leminh

I installed Nginx and Node.js in my server.

我在我的服务器中安装了 Nginx 和 Node.js。

When I try run my node.js file, I get an error:

当我尝试运行 node.js 文件时,出现错误:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: listen EADDRNOTAVAIL
    at errnoException (net.js:614:11)
    at Array.0 (net.js:689:28)
    at EventEmitter._tickCallback (node.js:192:40)

How can I fix this problem?

我该如何解决这个问题?

Thanks!

谢谢!

回答by JohnnyCoder

I had this issue, and I thought it was a portissue, but it turned out to be an IPissue.

我有这个问题,我以为是端口问题,但结果证明是IP问题。

I had my HOSTenvironmental variable pointing to a static IP, and my Node server was listening to that address. At some point when my router recycled, it started assigning my machine a new IP, and I started getting the error you posted. It was fixed by changing the address my server was listening to.

我的HOST环境变量指向一个静态 IP,我的 Node 服务器正在侦听该地址。在我的路由器回收的某个时候,它开始为我的机器分配一个新 IP,并且我开始收到您发布的错误。它是通过更改我的服务器正在侦听的地址来解决的。

My config:

我的配置:

app.set('port', process.env.PORT || 3000);
app.set('host', process.env.HOST || '0.0.0.0');

http.createServer(app).listen(app.get('port'), app.get('host'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

Make sure your host is hitting your machine. In any case, the loopback, as @miltonb suggested, should always work:

确保您的主机正在访问您的机器。无论如何,正如@miltonb 建议的那样,回送应该始终有效:

app.set('host', '127.0.0.1');

回答by Federico

I was getting the same error, and then I changed the port and worked

我遇到了同样的错误,然后我更改了端口并开始工作

回答by miltonb

I think you need a bit more context like some of your code. I am guessing you are starting to listen on a web server or a socket? Based on that assumption, I get something similar when I run a basic web server on my test server unless I run using localhost.

我认为你需要更多的上下文,比如你的一些代码。我猜你开始在网络服务器或套接字上收听?基于这个假设,除非我使用 localhost 运行,否则当我在测试服务器上运行基本 Web 服务器时,我会得到类似的结果。

events.js:48
        throw arguments[1]; // Unhandled 'error' event
                   ^
Error: listen EADDRNOTAVAIL
    at errnoException (net.js:670:11)
    at Array.0 (net.js:756:28)
    at EventEmitter._tickCallback (node.js:190:38)

Try changing the [hostname] parameter to localhost:

尝试将 [hostname] 参数更改为 localhost:

var http = require('http');
http.createServer( function ).listen(8000, '127.0.0.1');

回答by jonrh

Could it be that node was already running a server? I received a similar error and found this while troubleshooting. Shutting down the previous node server solved my problem.

是不是那个节点已经在运行服务器了?我收到了类似的错误,并在故障排除时发现了这个错误。关闭以前的节点服务器解决了我的问题。

回答by Vinoth

Most of the time it would be the IP address or the hostname. The reason being, node js takes that as the key item to start the server. if there is a conflict or incorrect ip address, it cribs with this error

大多数情况下,它是 IP 地址或主机名。原因是,node js 将其作为启动服务器的关键项。如果存在冲突或不正确的 IP 地址,则会出现此错误

Error: listen EADDRNOTAVAIL

错误:听 EADDRNOTAVAIL

hope this helps.

希望这可以帮助。

回答by Abdennour TOUMI

For me , i had the same error , and when i check my configuration , i found that host=127.0.0.0which raises error because it should be 127.0.0.1instead of 127.0.0.0

对我来说,我有同样的错误,当我检查我的配置时,我发现host=127.0.0.0它引发了错误,因为它应该是127.0.0.1而不是127.0.0.0

回答by Ricky Levi

For me, the issue was that the IP that I wrote simply didn't exists :) usually at home I have a 192.168.x.xIP, and after a restart I had a different address ... when I tried to gulp servemy app - it had a config with the old IP ....

对我来说,问题是我写的 IP 根本不存在 :) 通常在家里我有一个192.168.x.xIP,重启后我有一个不同的地址......当我尝试gulp serve我的应用程序时 - 它有一个配置用旧IP ....

as always 127.0.0.1will work, but when you want to verify your website with other devices, you want to use the external IP 192.168.x.x... or similar.

一如既往地127.0.0.1工作,但是当您想用其他设备验证您的网站时,您想使用外部 IP 192.168.x.x......或类似的。

回答by Alexandr Kashuba

Check your ip ipconfigadress and add any port 3004for example. Code below finally work for me. In case if you want to access this server from other device in your network. Or just set local host ip to 127.0.0.1and get access from your device.

例如,检查您的 IPipconfig地址并添加任何端口3004。下面的代码终于为我工作。如果您想从网络中的其他设备访问此服务器。或者只是将本地主机 ip 设置为127.0.0.1并从您的设备获取访问权限。

var server = require('http').createServer(app);
server.listen(3004, '192.168.x.x', function () {
  console.log("Listening on port 3000!");
});

回答by Master James

This also will occur while 'ip addr add i:p:v:6' is still in limbo or something (just executed/ing), and the interface is I presume not totally ready to listen and still busy adding the new ipv6 address.

当 'ip addr add i:p:v:6' 仍然处于不确定状态或其他东西(刚刚执行/正在执行)时,也会发生这种情况,并且我认为界面还没有完全准备好收听并且仍在忙于添加新的 ipv6 地址。

Using an execSync [see https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options]to call 'sleep' for a 2 second pause seems to allow listen to not error with Error: listen EADDRNOTAVAIL.

使用 execSync [参见https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options]来调用 'sleep' 暂停 2 秒似乎允许听没有错误错误:listen EADDRNOTAVAIL。

[It's really a bug I think in NodeJS 7.4.0 on Ubuntu because this doesn't happen with ipv4 address creation at the instant before usage/listen connection.]

[我认为这确实是 Ubuntu 上的 NodeJS 7.4.0 中的一个错误,因为在使用/侦听连接之前的瞬间创建 ipv4 地址时不会发生这种情况。]

回答by ofir_aghai

var express = require('express');

var app = express();
app.set('port', process.env.PORT || 3000);
app.set('host', process.env.HOST || 'localhost');

app.listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('host') + ':' + app.get('port'));
});

works for me.
it also generic for debug by localhostand your local ip without any changes.

为我工作。
它也适用于 debug bylocalhost和您的本地 ip,无需任何更改。