gpt4 book ai didi

node.js - 莫名其妙的 node.js http 抛出连接 ECONNREFUSED(IPv6?)

转载 作者:搜寻专家 更新时间:2023-10-31 22:37:10 27 4
gpt4 key购买 nike

我正在运行 node.js 如下:

> http = require('http')
> http.get('http://myhost.local:8080',
function (res) { console.log("RES" + res) }
).on('error', function (e) { console.log("Error:", e) })

> uri = require('url').parse("http://myhost.local:8080")
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'myhost.local:8080',
port: '8080',
hostname: 'myhost.local',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'http://myhost.local:8080/' }
> http.get(uri,
function (res) { console.log("RES" + res) }
).on('error', function (e) { console.log("Error:", e) })

隐式和显式解析的 URI 都抛出错误,我得到以下输出:

Error: { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' }

主机 myhost.local/etc/hostslocalhost 的别名,是:

127.0.0.1   localhost myhost.local myhost
255.255.255.255 broadcasthost
::1 localhost myhost.local myhost
fe80::1%lo0 localhost myhost.local myhost

编辑:我几乎尝试了主机文件的所有排列,包括最明显的排列:

127.0.0.1   localhost 
255.255.255.255 broadcasthost
::1 localhost myhost.local myhost
fe80::1%lo0 localhost

编辑 我还应该提一下,我现在已经在不止一台 Mac 上尝试过了。

虽然这似乎是一个相当常见的错误,但我没有看到任何有用的解释或解决方法。以下是一些值得注意的相关事实:

  1. 运行 $ wget http://myhost.local:8080 会按预期工作,因此这不是防火墙问题。
  2. 运行 $ telnet myhost.local 8080 然后手动获取 url 工作正常,所以这不是一个奇怪的 HTTP 问题。
  3. 我可以轻松使用 node.js 连接到其他主机,例如http://www.google.com

我希望有用的系统信息包括:

$ node -v
v0.9.11

$ uname -a
Darwin myhost.local 12.2.1 Darwin Kernel Version 12.2.1:
Thu Oct 18 12:13:47 PDT 2012; root:xnu-2050.20.9~1/RELEASE_X86_64 x86_64

$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.8.2
BuildVersion: 12C3104

$ sudo netstat -nalt | grep LISTEN | grep 8080
tcp6 0 0 ::1.8080 *.* LISTEN

有没有人知道这里发生了什么,以及可能的修复方法是什么?

最佳答案

我会在这里发布这个以防其他人遇到问题。

Bert Belder,Node.js 邮件列表:

On your system "myhost.local" resolves to three different addresses (127.0.0.1, ::1, and fe80::1). Node prefers ipv4 over ipv6 so it'll try to connect to 127.0.0.1. Nothing is listening on 127.0.0.1:8080 so the connect() syscall fails with ECONNREFUSED. Node doesn't retry with any of the other resolved IPs - it just reports the error to you. A simple solution would be to replace 'localhost' by the intended destination ip address, '::1'.

Whether this behavior is right is somewhat open for debate, but this is what causes it.

Bert

关于node.js - 莫名其妙的 node.js http 抛出连接 ECONNREFUSED(IPv6?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15227154/

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