node.js 当 URL 在浏览器中工作时,什么可能导致“connect ETIMEDOUT”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33350604/
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 19:25:28  来源:igfitidea点击:

What could cause "connect ETIMEDOUT" error when the URL is working in browser?

node.jshttp

提问by Alex

I train myslef with NodeJS and tried a simple GET call. Here is my code:

我用 NodeJS 训练 myslef 并尝试了一个简单的 GET 调用。这是我的代码:

var http = require('http');

var options = {
    host: 'www.boardgamegeek.com',
    path: '/xmlapi/boardgame/1?stats=1',
    method: 'GET'
}

var request = http.request(options, function (response) {
    var str = ""
    response.on('data', function (data) {
        str += data;
    });
    response.on('end', function () {
        console.log(str);
    });
});

request.on('error', function (e) {
    console.log('Problem with request: ' + e.message);
});

request.end();

The URL called seems to work in my browser https://www.boardgamegeek.com/xmlapi/boardgame/1?stats=1

调用的 URL 似乎在我的浏览器中有效https://www.boardgamegeek.com/xmlapi/boardgame/1?stats=1

Anyway, I've got Problem with request: connect ETIMEDOUTwhen I run the code and I have no idea how to fix it.

无论如何,我遇到了请求问题:当我运行代码时连接 ETIMEDOUT,我不知道如何修复它。

What could cause this error ? Is it my code or is it a network/proxy issue?

什么可能导致此错误?是我的代码还是网络/代理问题?

回答by sdabet

When behind a proxy you need to make the following modifications (as explained in this answer):

在代理后面时,您需要进行以下修改(如本答案中所述):

  • put the proxy host in the hostparameter
  • put the proxy port in the portparameter
  • put the full destination URL in the pathparameter :
  • 将代理主机放在host参数中
  • 将代理端口放在port参数中
  • 将完整的目标 URL 放在path参数中:

Which gives:

这使:

var options = {
    host: '<PROXY_HOST>',
    port: '<PROXY_PORT>',
    path: 'http://www.boardgamegeek.com/xmlapi/boardgame/1?stats=1',
    method: 'GET',
    headers: {
        Host: 'www.boardgamegeek.com'
    }
}

回答by hema

The following change with the request worked for me:

请求的以下更改对我有用:

 var options = {
         proxy:'PROXY URL', 
         uri: 'API URL',
         method: 'GET' 
         }
 request(options, function (err, response, body) {
     if(err){
        console.log('error:', err);
       } else {
     console.log('body:', body);
      }
   });

回答by M.Vanderlee

In my case it was a misconfigured subnet. Only one of the 2 subnets in the ELB worked. But my client kept trying to connect to the misconfigured one.

就我而言,这是一个配置错误的子网。ELB 中的 2 个子网中只有一个有效。但是我的客户一直试图连接到配置错误的那个。

回答by Rishikesh Chandra

I was facing this issue on Ubuntu Server while maintaining a node instance on PM2. Basically after restarting the instance after taking the pull I was getting the same error on initial connection of mySQL inside the code.

我在 Ubuntu Server 上遇到了这个问题,同时在 PM2 上维护了一个节点实例。基本上在拉取后重新启动实例后,我在代码中初始连接 mySQL 时遇到相同的错误。

Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/home/deam_server/node_modules/mysql/lib/Connection.js:419:13)
at Object.onceWrapper (events.js:275:13)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:442:20)
at Socket._onTimeout (net.js:447:8)
at ontimeout (timers.js:427:11)
at tryOnTimeout (timers.js:289:5)
at listOnTimeout (timers.js:252:5)
at Timer.processTimers (timers.js:212:10)

Though the same code was running perfectly on my local machine. After that I used "df" command which helped me to realise that my root directory was 100% occupied. I allotted some memory to the root directory and the restarted the node instance and then it started working fine.

尽管相同的代码在我的本地机器上完美运行。之后我使用了“df”命令,它帮助我意识到我的根目录被 100% 占用。我为根目录分配了一些内存并重新启动了节点实例,然后它开始正常工作。