2013-10-21 65 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

無需在任何地方輸入設置,您可以直接將代理字符串分配給代理字段。 –

相關問題