Node.js读取ECONNRESET

时间:2018-04-02 11:58:03

标签: node.js express post econnreset

我正在为我的应用程序使用Express.js,并在向adobe analytics API发布请求时收到错误。

我尝试添加server.timeout,但它没有修复它......

这是错误消息:

  

错误:读取ECONNRESET
      at exports._errnoException(util.js:1028:11)
      在TLSWrap.onread(net.js:572:26)代码:' ECONNRESET',错误:' ECONNRESET',系统调用:'阅读'

这是失败的代码:

  pullClassifications(req) {
    const dfd = q.defer();
    const { username, password, payload } = req;
    const data = wsse({username: username, password: password});
    const wsseHeaders = {'X-WSSE': data.toString({ nonceBase64: true })};
    const options = {
      'method': 'post',
      'headers': wsseHeaders,
      'content-type': 'application/json',
      'body': payload,
      'json': true,
      'url': 'https://api.omniture.com/admin/1.4/rest/?method=ReportSuite.GetClassifications'
    }

    request(options, function(err, response, body) {
      if(response.statusCode == 200) {
        dfd.resolve(body);
      } else {
        dfd.reject({statusCode: response.statusCode, body: body});
      }
    })
    return dfd.promise;
  }

更新:
我试图使用Postman发布相同的请求并且它可以工作,在630000毫秒内返回响应。

导致此错误的原因是什么?有什么方法可以修复它吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试在请求中添加timeout选项?当你使用本机承诺时,为什么要使用Q promise库?旧NodeJs版本?

https://github.com/request/request#timeouts

    const options = {
      'timeout': 120 * 60 * 1000 //(120 seconds)
      'method': 'post',
      'headers': wsseHeaders,
      'content-type': 'application/json',
      'body': payload,
      'json': true,
      'url': 'https://api.omniture.com/admin/1.4/rest/?method=ReportSuite.GetClassifications'
    }

    request(options, function(err, response, body) {...});