【发布时间】:2021-09-25 02:02:45
【问题描述】:
所以,我有一个我在本地测试过的 nodeJS http 服务器,它可以工作。但是当我在外部尝试时,它说“连接超时”
这是我的服务器代码:
const http = require('http');
const requestListener = function (req, res) {
console.log(req);
res.writeHead(200);
res.end('NodeJS server testing.');
}
const server = http.createServer(requestListener);
server.listen(8080, "0.0.0.0", () => {
console.log("http server listening");
});
我试过输入后显示的 IP 地址:
hostname -I
而且它有效。
$ curl [my-local-hostname]:8080
NodeJS server testing.#
但是IP地址来自
$ curl ifconfig.co
或者,
$ curl ifconfig.me
没用。
$ curl [my-external-hostname]:8080
curl: (28) Failed to connect to 37.111.219.65 port 8080: Connection timed out
我也尝试过其他设备。但它不起作用。 有什么解决办法吗?
【问题讨论】:
-
您服务器的 8080 端口是否对入站流量开放?
-
怎么做(不使用很多库?)
-
这完全取决于您的服务器。如果您使用的是 AWS ec2 实例,请转到该实例的安全组并在入站规则中允许端口 8080 上的流量。