2015-10-26 22 views
3

我使用NodeJS訓練myslef並嘗試了一個簡單的GET調用。 這裏是我的代碼:URL在瀏覽器中工作時,可能會導致「連接ETIMEDOUT」錯誤?

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當我運行的代碼,我沒有想法如何解決它。

什麼可能導致此錯誤?它是我的代碼還是網絡/代理問題?

+1

你是否支持代理? – fiddler

+0

是的,我有一個公司代理! – Alex

回答

3

落後時,你需要做如下修改(如this answer解釋)代理:

  • 把代理主機在host參數
  • 把代理端口在port參數
  • 將完整的目標網址放入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' 
    } 
} 
+0

謝謝!有用。我不得不在路徑和主機頭中添加http,但不能通過代理工作。 – Alex

+0

好。感謝編輯。 – fiddler

相關問題