node.js 的 ECONNREFUSED 错误不会在其他客户端发生

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10643965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:43:03  来源:igfitidea点击:

ECONNREFUSED error with node.js that does not occur in other clients

httpnode.js

提问by aaronstacy

I'm making an httprequest with the node.jsclient, and I get an ECONNREFUSEDerror. When I make what appears to be the same request with my browser or curl(1), it works just fine

我正在httpnode.js客户提出请求,但ECONNREFUSED出现错误。当我用浏览器或 发出看似相同的请求时curl(1),它工作得很好

Here's the node request:

这是节点请求:

var options = {
  host: 'localhost',
  port: 8080,
  path: '/explorers/1.0/agegroup',
  method: 'GET'
};

var req = http.request(options, function(res) {
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log(e);
});

req.end();

And it gives me the error:

它给了我错误:

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

But when I make the same request with a different client (curlin this case):

但是当我向不同的客户端发出相同的请求时(curl在这种情况下):

$ curl http://localhost:8080/explorers/1.0/agegroup
{... response JSON ...}

Other notes:

其他注意事项:

  • I've tried changing the host to www.google.com and the port to 80, and node makes the connection successfully

  • I've tried changing the port of the server, and I can still make requests with all clients but node.js (which still gets ECONNREFUSED)

  • The server to which I'm connecting is the CherryPy WSGI Server. When I try connecting to a nodeserver at localhost:8080, it works fine, which would make me think it's the server's problem, except other clients work with the CherryPy server.

  • I've tried using the same headers that my browser is using, but that doesn't work, and it seems like the problem is at the TCP level anyway, so HTTP headers shouldn't be an issue.

  • 我已经尝试将主机更改为 www.google.com 并将端口更改为 80,并且节点成功建立连接

  • 我已经尝试更改服务器的端口,但我仍然可以向除 node.js 之外的所有客户端发出请求(仍然得到ECONNREFUSED

  • 我要连接的服务器是CherryPy WSGI Server。当我尝试连接到nodelocalhost:8080的服务器时,它工作正常,这让我认为这是服务器的问题,除了其他客户端使用 CherryPy 服务器。

  • 我已经尝试使用浏览器正在使用的相同标头,但这不起作用,而且似乎问题出在 TCP 级别,因此 HTTP 标头应该不是问题。

What's wrong with my noderequest?

我的node请求有什么问题?

采纳答案by aaronstacy

I guess sometimes you just need to step away from the problem...

我想有时你只需要远离问题......

I found a solution, but it doesn't seem to answer the question, and I don't really like the it.

我找到了一个解决方案,但它似乎没有回答这个问题,而且我真的不喜欢它。

I changed the CherryPy server configuration to serve at 127.0.0.1instead of localhost, and the nodeclient started working.

我将 CherryPy 服务器配置更改为服务于 at127.0.0.1而不是localhost,并且node客户端开始工作。

回答by Steveorevo

I disagree with Soman's disagreement.

我不同意 Soman 的不同意见。

I've been pulling my hair out on this one, but yes, sure enough nodejs v0.10.24 and v0.10.25 refuse to connect to a PHP 5.4 development server (PHP's command line server) when invoked as:

我一直在研究这个问题,但是是的,当被调用为时,nodejs v0.10.24 和 v0.10.25 确实拒绝连接到 PHP 5.4 开发服务器(PHP 的命令行服务器):

php -S localhost:8088

The browser, curl, everything else connects just fine. Yet, sure enough, changing the binding address to be:

浏览器,curl,其他一切都连接得很好。然而,果然,将绑定地址更改为:

php -S 127.0.0.1:8088 

And nodejs' using xmlrpc connects just fine. This occurred on a Mac OS X 10.9.1. Very odd.

而 nodejs' 使用 xmlrpc 连接就好了。这发生在 Mac OS X 10.9.1 上。很奇怪。

回答by Txangel

I just faced the same problem.

我刚刚面临同样的问题。

Note that Windows 7 by default doesn't include an entry for localhost in C:\Windows\System32\drivers\etc\hosts(wikipedia reference)

请注意,默认情况下,Windows 7 中不包含 localhost 条目C:\Windows\System32\drivers\etc\hosts维基百科参考

Adding the entry for localhost has no effect with Node.js (0.10.25) as it apparently skips the hosts file completely.

为 localhost 添加条目对 Node.js (0.10.25) 没有影响,因为它显然完全跳过了主机文件。

Use 127.0.0.1 as local address instead, this will works what ever is in your hosts file.

使用 127.0.0.1 作为本地地址,这将适用于您的主机文件中的任何内容。

回答by myst552

I had the same problem. I was able to resolve it by replacing the localhost or 127.0.0.1 by my system's ip address(use ifconfig for the ip address).

我有同样的问题。我能够通过用我系统的 ip 地址替换 localhost 或 127.0.0.1 来解决它(使用 ifconfig 作为 ip 地址)。

回答by Soman Dubey

I exactly faced the same issue where I was accessing a third party url. I tried using dns (subdomain.domain.com) and ip (XXX.XXX.XXX.XXX) both but it did not work.

我在访问第三方网址时遇到了同样的问题。我尝试使用 dns (subdomain.domain.com) 和 ip (XXX.XXX.XXX.XXX) 但它没有用。

After some struggle, later I realized that this was a problem at server (subdomain.domain.com) end which was not responsive and gone down.

经过一番挣扎,后来我意识到这是服务器(subdomain.domain.com)端的一个问题,它没有响应并出现故障。

I disagree this is a problem with localhost / 127.0.0.1 (or domain name vs IP address). ( In above case, it's surely a case of incorrect host mapping)

我不同意这是 localhost / 127.0.0.1(或域名与 IP 地址)的问题。(在上面的情况下,肯定是主机映射不正确的情况)