nodejs请求ECONNREFUSED

时间:2017-02-27 15:13:55

标签: node.js httprequest

我有一些ECONNREFUSED带有'request'模块,但有时候我的请求没有错误O_o ... 所以我让我的请求反复出现,但这并没有解决问题......

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' } ]

1 个答案:

答案 0 :(得分: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标题,但如果你传递一个标题可能会更好:

request({ url, headers : { Host : 'api.cryptowat.ch' } }, callbackRequest);