1
我今天遇到这种奇怪的行为,我找不到原因。我正在使用MacOS Sierra。为什么Node.js/Express不接受本地主机的连接?
我有这样的代码(快递):
app.server.listen(config.port, config.address, function() {
logger.info('app is listening on', config.address + ':' + config.port);
});
而且它打印
app is listening on 127.0.0.1:5000
如何过,如果我尝试curl
,它失败。
$ curl http://localhost:5000/api/ping
curl: (56) Recv failure: Connection reset by peer
我检查了我的hosts文件:
$ cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
所以我ping本地主机,以确保它解析为127.0.0.1
:
$ ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.061 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.126 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.135 ms
^C
--- localhost ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.061/0.107/0.135/0.033 ms
我再试一次,但失败
$ curl http://localhost:5000/api/ping
curl: (56) Recv failure: Connection reset by peer
现在我试着给我们反而瞧,它的作品?
$ curl http://127.0.0.1:5000/api/ping
pong
怎么了?
你介意发布/ etc/hosts文件的内容吗?在我看来,'localhost'可能被错误地映射到那里。这可能会导致这种错误。 –
您的网络适配器是否启用了IPv6? Curl可能试图通过IPv6连接,但服务器不在该地址上进行监听。 –
添加主机文件 – mitchkman