2017-02-17 45 views
0

我正嘗試使用請求模塊調用外部API,當調用本地託管的API但在調用外部API時沒有成功時,它正常工作。注:我身後代理這可能是問題使用請求模塊在代理後面調用外部REST api

代碼

app.get('/Get', function(req, res){ 

request.get('https://api.github.com/repos/request/request', 

function(error, response, body) { 

if (!error && response.statusCode === 200) { 
     res.send(response.body);} 
else { 
     res.json(error); 
    } 
}); 
}); 
app.listen(8080); 
console.log("server is running at 8080"); 

它是否工作正常,如果我打電話給當地的API波紋管:

request.get('http://localhost:8080/hello', 

回答

0

代理是問題,PFB解決方案如何設置代理並添加x-api-key標頭

request({uri:'https://api.github.com/repos/request/request', proxy:'http://proxy.server.com', 
headers:{ 
'x-api-key':'asdfasdfsda'} 
},