2014-09-03 55 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' 
}