嗨我想调用一个简单的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)
任何一个能告诉我出了什么问题吗?
对我的作品中设置代理再试一次。检查您的互联网连接。 –
我试图在我的办公室网络中这么做......我认为这有一些限制......它在我自己的网络上为我工作。 –
你可能必须使用代理服务器:http://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client – phoet