以下是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!
答案 0 :(得分:3)
我从
更改了代码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.');
});
现在它在工作
答案 1 :(得分:1)
这对我有用!
在安全组中,添加了HTTP规则,该规则默认在端口80上进行侦听。
因此,基本上,如果您已将节点服务器配置为在端口号80以外的端口上运行(我在此错误),并尝试在浏览器上访问公共DNS(在实例描述中可以找到EC2公共DNS),可能会出现连接拒绝错误,因此您可以将配置中的PORT值更改为80。
您的config.env看起来像这样
PORT=80
在您的server.js中,您可以编写
const PORT = process.env.PORT;
try {
app.listen(PORT, () => { console.log(`server running at port ${PORT}`) })
} catch (error) {
console.log(error)
}