当我尝试执行简单的Get请求时出现ETIMEDOUT错误?

时间:2013-10-21 07:14:45

标签: node.js http

您好我正在尝试调用一个简单的Web API,它返回一个字符串作为响应。我想为此使用节点。由于我是节点的新用户,所以我尝试了许多博客文章,并获得了我使用的代码片段,但无论是google.com还是其他任何内容,我都会收到相同的错误。

我的节点代码如下

var http = require('http');

//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
  host: 'www.random.org',
  path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};

callback = function(response) {
  var str = '';

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });
}

http.request(options, callback).end();

错误:

F:\nodejs>node ..\NodeLearning\TestServer1\test.js

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ETIMEDOUT
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

任何人都可以告诉我这里出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

您可以通过设置如下所述的代理再试一次

var options = {
  host: 'www.random.org',
  path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new',
  proxy:'add your proxy setting' 
};
相关问题