这里是app.js,代码太长所以我只显示这段代码,其他代码没有问题我认为这是网络问题。
app.js
app.listen(8080, 'localhost', function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
当我运行 lsof -i :8080
时,我没有得到任何响应。但是当我在服务器上运行 curl localhost:8080
时,我确实得到了响应。
而且我认为安全组没有任何问题。我允许任何 ip 访问实例,如下所示。
这实际上是我测试 public ip
和 localhost
ubuntu@:ip~/$ curl -v 18.217.107.76:8080
* Rebuilt URL to: 18.217.107.76:8080/
* Trying 18.217.107.76...
* connect to 18.217.107.76 port 8080 failed: Connection refused
* Failed to connect to 18.217.107.76 port 8080: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 18.217.107.76 port 8080: Connection refused
ubuntu@ip:~/$ curl -v localhost:8080
I get response here!
最佳答案
我更改了代码
app.listen(8080, 'localhost', function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
到
app.listen(8080, function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
现在可以了
关于node.js - 无法连接到 Amazon EC2 上的 Node 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48349056/