【问题标题】:Node js Error: listen EADDRNOTAVAIL 52.**.**.***:1122节点 js 错误:监听 EADDRNOTAVAIL 52.**.**.***:1122
【发布时间】:2019-05-26 02:29:34
【问题描述】:

Node js 应用程序在本地运行良好。尝试在服务器中运行时显示错误。

ubuntu@ip-172-*-*-*:~/sample_nodejs$ node main.js
events.js:183
throw er; // Unhandled 'error' event
  ^

Error: listen EADDRNOTAVAIL 52.*.*.*:1122
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1350:19)
at listenInCluster (net.js:1408:12)
at doListen (net.js:1517:7)
at _combinedTickCallback (internal/process/next_tick.js:141:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3

这是我的端口运行状态。使用的命令 netstat -tulpn

(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      
-
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      
-
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
-
tcp6       0      0 :::22                   :::*                    LISTEN      
-
tcp6       0      0 :::80                   :::*                    LISTEN      
-
udp        0      0 127.0.0.53:53           0.0.0.0:*                           
-
udp        0      0 172.*.*.*:68          0.0.0.0:*                           -

尝试更改 iptables

sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

仍然显示错误。这是服务器故障还是我需要服务器端的许可?

这是我尝试使用公共 IP 地址的示例代码:

   var express = require('express');

  var app = express();

 const http = require('http');

  const hostname = '52.*.*.*';

  const port = 1122;

  const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello, World!\n');
  });

  server.listen(port, hostname, () => {
   console.log(`Server running at http://${hostname}:${port}/`);
   });

【问题讨论】:

标签: node.js amazon-web-services ubuntu networking server


【解决方案1】:

不要尝试侦听 EC2 实例上的外部 IP 地址 - 它不可用。相反,请在“任何”接口(有时显示为0.0.0.0)上收听。根据文档,我可以发现这是使用 server.listen 完成的,没有指定主机参数:

如果省略host,服务器将接受 unspecified IPv6 address (::) 当 IPv6 可用时,或者 unspecified IPv4 address (0.0.0.0) 否则。

在大多数操作系统中,收听unspecified IPv6 address (::) 可能会导致net.Server 也监听unspecified IPv4 address (0.0.0.0)。

您侦听的端口将可以从外部世界访问,尽管您必须使用安全组公开它。

编辑

您需要将代码更改为:

var express = require('express');

var app = express();

const http = require('http');

const port = 1122;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello, World!\n');
});

server.listen(port, () => {
  console.log(`Server running at on port ${port}`);
});

【讨论】:

  • 感谢您的回答。嘿,我是新手。 cna 你检查我迄今为止尝试过的示例代码。如何在浏览器中查看?
【解决方案2】:

正如stundbar所说,做服务器的时候,不能设置公网IP,只能设置0.0.0.0或者localhost。 不要设置 127.0.0.1,因为您将无法在服务器机器之外访问它。

【讨论】:

  • 感谢您的回答。嘿,我是新手。 cna 你检查我迄今为止尝试过的示例代码。如何在浏览器中查看?
  • 当然。你有意见不和吗?
  • 好的,给我发邮件到 darkscientist@hynity.com
  • 嘿,我已经安装了 discord
  • 好的,加我为好友:DarkScientist_#9449
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多