gpt4 book ai didi

node.js - Node js错误: listen EADDRNOTAVAIL 52. **.**.***:1122

转载 作者:太空宇宙 更新时间:2023-11-04 03:01:44 25 4
gpt4 key购买 nike

本地的 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

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

这是我尝试使用公共(public) 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}/`);
});

最佳答案

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

If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.

In most operating systems, listening to the unspecified IPv6 address (::) may cause the net.Server to also listen on the 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}`);
});

关于node.js - Node js错误: listen EADDRNOTAVAIL 52. **.**.***:1122,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53955562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com