【问题标题】:Node.js read ECONNRESETNode.js 读取 ECONNRESET
【发布时间】:2018-09-11 15:30:14
【问题描述】:

我在我的应用程序中使用 Express.js,向 adobe 分析 API 发出发布请求时出现错误。

我尝试添加server.timeout,但没有解决它...

这是错误信息:

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

这是失败的代码:

  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 毫秒内返回响应。

这个错误的原因是什么,有什么办法可以解决吗?

【问题讨论】:

  • 该错误可能与 Node.js/https 模块无关。我建议你在 Node.js 之外尝试一下(例如使用 curl 或 Postman 之类的东西)
  • @fardjad 我尝试使用 Postman 并且它有效。总时间为 630000ms
  • 这似乎是 API 方面的问题
  • @RenatoGama 但为什么它使用 Postman 工作?
  • 这里有同样的问题。适用于邮递员,但不适用于我的后端。

标签: node.js express post econnreset


【解决方案1】:

您是否尝试在请求中添加timeout 选项?当你可以使用原生 Promise 时,为什么还要使用 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) {...});

【讨论】:

  • 我尝试将timeout 选项添加到请求中,但仍然无法正常工作。我没有使用旧的 NodeJs 版本,只是整个项目使用了 Q Promise。
  • 执行timeout: 0 会禁用请求超时吗?
猜你喜欢
  • 2022-01-02
  • 1970-01-01
  • 2018-03-21
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 2018-06-21
相关资源
最近更新 更多