2017-02-27 104 views
0

我有一些ECONNREFUSED與「請求」模塊,但有些時候我的請求過時沒有錯誤O_O ... 所以我讓我的請求recurcive但這不能解決problème...要求的NodeJS ECONNREFUSED

let request  = require("request"); 
let currency = 'btceur'; 
let data = []; 
let url    = "https://api.cryptowat.ch/markets/kraken/" + currency + "/price"; 
let nbTry   = 0; 
let nbMaxTry  = 5; 
let callbackRequest = (error, response, body) => { 
    if (error || response.statusCode != 200) { 
     console.log('error', 'error, retry ' + (nbTry + 1) + "/" + nbMaxTry); 
     console.log(error); 
     if (nbTry <= nbMaxTry) { 
      nbTry++; 
      request(url, callbackRequest); 
     } else { 
      console.log(data); 
     } 
    } else { 
     let bodyjson    = JSON.parse(body); 
     bodyjson.result.currency = currency; 
     data.push(bodyjson.result); 
     console.log(data); 
    } 
}; 
request(url, callbackRequest); 

控制檯輸出:

error error, retry 1/5 
{ Error: connect ECONNREFUSED 69.164.196.116:443 
    at Object.exports._errnoException (util.js:1022:11) 
at exports._exceptionWithHostPort (util.js:1045:20) 
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14) 
    code: 'ECONNREFUSED', 
    errno: 'ECONNREFUSED', 
    syscall: 'connect', 
    address: '69.164.196.116', 
    port: 443 } 
[ { price: 1113.5, currency: 'btceur' } ] 

回答

2

api.cryptowat.ch解析爲兩個IP號碼,23.239.28.55和69.164.196.116。後者給出的問題(對我來說也是如此)。

你可以嘗試使用前者爲每個請求作爲臨時解決方法:

let url = "https://23.239.28.55/markets/kraken/" + currency + "/price"; 

它似乎並不需要一個Host頭,雖然它可能會更好,如果你通過一個反正:

​​
+0

是的,恆定的IP解決了與Promise.all類似的問題。 –