2017-09-08 140 views
2

我是nodejs和express的新手。我正在使用「請求」庫來閱讀google.com的內容。我不斷收到連接拒絕錯誤。我把代碼https://github.com/request/request節點請求返回ECONNREFUSED

這是我first_request.js文件:

var request = require('request'); 
    request('http://www.google.com', function (error, response, body) { 
    console.log('error:', error); // Print the error if one occurred 
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
    console.log('body:', body); // Print the HTML for the Google homepage. 
}); 

要運行,我到該文件夾​​並做node first_request.js

它給我的錯誤是:

enter image description here

我在做什麼錯?任何指針?

回答

2

當您設置代理時會發生此錯誤。該鏈接在我的電腦上正常工作。您可以使用request.defaults({})來設置您的代理選項。您應該指定用戶憑據+主機和端口。

var request = require('request'); 

var link = "http://www.google.com"; 

var proxyUrl = "http://" + user + ":" + password + "@" + host + ":" + port; 

var proxiedRequest = request.defaults({'proxy': proxyUrl}); 

proxiedRequest(link , function (error, response, body) { 
    //handle the response.. 
})