2014-09-03 63 views
0

錯誤簡單的HTTP請求,下面的錯誤代碼

Error: { [Error: connect ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect' } 
hostName: 'api.v3.factual.com'     
url : http://api.v3.factual.com/t/places?geo=%22$point%22:12.9361149,77.6159516]}&q=Cool%20Roll%20Burger&KEY=ApiKey 

代碼

function internalHttpApiCall(hostName,url,callback) 
{ 

    var options = { 
    hostname : hostName,  
    port: 4456, 
    path: url, 
    method: 'GET', 
    } 

/* 類似的HTTPS調用工作就好了。 我已採取以下鏈接作爲解決問題的參考。 How can I use an http proxy with node.js http.Client? How can I use an http proxy with node.js http.Client? */

var response=""; 

    var req = http.request(options, function(res) { 
    console.log("statusCode: ", res.statusCode); 
    console.log("headers: ", res.headers); 
    console.log(options.path); 

    res.on('data', function(d) { 
     response += d; 
    }); 

    res.on('end', function() { 
     if (response && JSON.parse(response)) 
     { 
      var obj = JSON.parse(response) 
      console.log(obj); 
      output(); 
     } 
    }); 

    }); 

    req.end(); 

    req.on('error', function(e) { 
     console.error(e); 
    }); 

    function output(xresponse) 
    { 
     callback(xresponse); 
    } 
} 
+0

'host'而不是'hostname'? – DanFromGermany 2014-09-03 10:14:12

+0

是的,我發佈之前嘗試過。仍然不起作用。 – 2014-09-03 10:21:19

回答

0

剛從options對象中刪除port屬性附加傷害。當您將port傳遞給options對象時,您說您要對該特定端口發出請求,如http://api.v3.factual.com:4456。由於他們只公開他們的URL,我們不知道他們在幕後使用哪個端口。因此,這不是必要的。

並且在method: 'GET'選項後刪除錯過的,

var options = { 
    hostname: hostName, 
    path: url, 
    method: 'GET' 
}