当URL在浏览器中工作时,可能会导致“连接ETIMEDOUT”错误?

我用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(); 

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

无论如何,我有请求的问题:连接ETIMEDOUT当我运行的代码,我不知道如何解决它。

什么可能导致这个错误? 是我的代码还是networking/代理问题?

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

  • 将代理主机放在host参数中
  • 把代理端口放在port参数中
  • 将完整的目标url放在path参数中:

这使:

 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' } }