2013-10-21 57 views
0

嗨我想调用一个简单的Web API,它返回一个字符串作为响应。我想为此使用节点。由于我是节点新手,所以我尝试了解许多博客文章,并获得了我使用的代码片段,但是对于所有网址(无论是google.com还是其他网址),我都收到了相同的错误。当我尝试执行简单的Get请求时,发生ETIMEDOUT错误?

节点代码如下

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

我试图在我的办公室网络中这么做......我认为这有一些限制......它在我自己的网络上为我工作。 –

+0

你可能必须使用代理服务器:http://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client – phoet

回答

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' 
}; 
+0

你在哪里输入设置? –

+0

无需在任何地方输入设置,您可以直接将代理字符串分配给代理字段。 –

相关问题