0
我是node.js中的一個begginer,現在我試圖從http.get獲取API的結果。但是,當運行代碼時,我收到此錯誤:嘗試在node.js中使用http.get訪問API時出錯
Error: getaddrinfo ENOTFOUND
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
我需要正確的HTTP(我不得不測試它,以確保) 這裏是我的功能來說明這個問題:
function getData(apiUrl, getUrl) {
var options = {
host : apiUrl,
port : 80,
path : getUrl
};
var content = "";
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
content += chunk;
console.log(content);
});
}).on('error', function(e) {
console.log("Error: " + e.message);
console.log(e.stack);
});
}
,把它這種方式:
getData('https://api.twitter.com', '/1/statuses/user_timeline.json?&screen_name=twitter&callback=?&count=1');
希望你能幫助我,謝謝!
總是嘗試使用'hostname'而不是'host',因此您可以使用'url.parse()'(按照[文檔](http://nodejs.org/api/http.html#) http_http_request_options_callback))。 – Chad 2013-03-24 01:20:56
謝謝你們兩位!現在它正在運行! – fnnrodrigo 2013-03-24 01:24:48
感謝您的評論乍得。我相應地編輯了我的答案。 – 2013-03-24 01:32:38