NodeJs ECONNRESET

NodeJs ECONNRESET

我在执行 GET 请求时遇到错误。

Error: read ECONNRESET
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }

当我尝试向 https://www.adidas.co.uk/api/search/taxonomy?query=men 发出 GET 请求时会发生这种情况,但当我使用 https://jsonplaceholder.typicode.com/api/todos/1.

时它会起作用

要求:

var request = require('request');
[...]
app.use('/api', cacheMiddleware(), function (req, out) {


      var headers = {
        //'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0',
        'Content-Type': 'application/x-www-form-urlencoded'
        //'Content-Type': 'application/jsond'
      };

      var url = getDestinationUrl(req);
      if (req.method === 'POST') {
        console.log('POST body:')
        console.log(req.body);
        result = request.post({ uri: url, json: req.body, headers: headers, followRedirect: true , maxRedirects: 10}, function (error, response, body) { handleResponse(req, out, error, response, body) });
      } else {
        result = request({ uri: url, headers: headers, followRedirect: true,  maxRedirects: 10 }, function (error, response, body) { handleResponse(req, out, error, response, body) });
      }

    });

日志:

    handle https://www.adidas.co.uk/api/search/taxonomy?query=men
REQUEST { uri: 'https://www.adidas.co.uk/api/search/taxonomy?query=men',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  followRedirect: true,
  maxRedirects: 10,
  callback: [Function] }
HTTP 10384: SERVER socketOnParserExecute 739
REQUEST make request https://www.adidas.co.uk/api/search/taxonomy?query=men
HTTP 10384: call onSocket 0 0
HTTP 10384: createConnection www.adidas.co.uk:443:::::::::::::::: { servername: 'www.adidas.co.uk',
  _defaultAgent:
   Agent {
     _events: [Object: null prototype] { free: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     defaultPort: 443,
     protocol: 'https:',
     options: { path: null },
     requests: {},
     sockets: { 'www.adidas.co.uk:443::::::::::::::::': [] },
     freeSockets: {},
     keepAliveMsecs: 1000,
     keepAlive: false,
     maxSockets: Infinity,
     maxFreeSockets: 256,
     maxCachedSessions: 100,
     _sessionCache: { map: {}, list: [] } },
  _events:
   [Object: null prototype] {
     error: [Function: bound ],
     complete: [Function: bound ],
     pipe: [Function] },
  _eventsCount: 3,
  _maxListeners: undefined,
  uri:
   Url {
     protocol: 'https:',
     slashes: true,
     auth: null,
     host: 'www.adidas.co.uk',
     port: 443,
     hostname: 'www.adidas.co.uk',
     hash: null,
     search: '?query=men',
     query: 'query=men',
     pathname: '/api/search/taxonomy',
     path: '/api/search/taxonomy?query=men',
     href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
  headers:
   { 'Content-Type': 'application/x-www-form-urlencoded',
     host: 'www.adidas.co.uk' },
  followRedirect: true,
  maxRedirects: 10,
  callback: [Function],
  readable: true,
  writable: true,
  _qs:
   Querystring {
     request:
      Request {
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        uri: [Url],
        headers: [Object],
        followRedirect: true,
        maxRedirects: 10,
        callback: [Function],
        readable: true,
        writable: true,
        _qs: [Circular],
        _auth: [Auth],
        _oauth: [OAuth],
        _multipart: [Multipart],
        _redirect: [Redirect],
        _tunnel: [Tunnel],
        setHeader: [Function],
        hasHeader: [Function],
        getHeader: [Function],
        removeHeader: [Function],
        method: 'GET',
        localAddress: undefined,
        pool: {},
        dests: [],
        __isRequestRequest: true,
        _callback: [Function],
        proxy: null,
        tunnel: true,
        setHost: true,
        originalCookieHeader: undefined,
        _disableCookies: true,
        _jar: undefined,
        port: 443,
        host: 'www.adidas.co.uk',
        path: '/api/search/taxonomy?query=men',
        httpModule: [Object],
        agentClass: [Function],
        agent: [Agent],
        _started: true,
        href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
     lib:
      { formats: [Object], parse: [Function], stringify: [Function] },
     useQuerystring: undefined,
     parseOptions: {},
     stringifyOptions: {} },
  _auth:
   Auth {
     request:
      Request {
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        uri: [Url],
        headers: [Object],
        followRedirect: true,
        maxRedirects: 10,
        callback: [Function],
        readable: true,
        writable: true,
        _qs: [Querystring],
        _auth: [Circular],
        _oauth: [OAuth],
        _multipart: [Multipart],
        _redirect: [Redirect],
        _tunnel: [Tunnel],
        setHeader: [Function],
        hasHeader: [Function],
        getHeader: [Function],
        removeHeader: [Function],
        method: 'GET',
        localAddress: undefined,
        pool: {},
        dests: [],
        __isRequestRequest: true,
        _callback: [Function],
        proxy: null,
        tunnel: true,
        setHost: true,
        originalCookieHeader: undefined,
        _disableCookies: true,
        _jar: undefined,
        port: 443,
        host: 'www.adidas.co.uk',
        path: '/api/search/taxonomy?query=men',
        httpModule: [Object],
        agentClass: [Function],
        agent: [Agent],
        _started: true,
        href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
     hasAuth: false,
     sentAuth: false,
     bearerToken: null,
     user: null,
     pass: null },
  _oauth:
   OAuth {
     request:
      Request {
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        uri: [Url],
        headers: [Object],
        followRedirect: true,
        maxRedirects: 10,
        callback: [Function],
        readable: true,
        writable: true,
        _qs: [Querystring],
        _auth: [Auth],
        _oauth: [Circular],
        _multipart: [Multipart],
        _redirect: [Redirect],
        _tunnel: [Tunnel],
        setHeader: [Function],
        hasHeader: [Function],
        getHeader: [Function],
        removeHeader: [Function],
        method: 'GET',
        localAddress: undefined,
        pool: {},
        dests: [],
        __isRequestRequest: true,
        _callback: [Function],
        proxy: null,
        tunnel: true,
        setHost: true,
        originalCookieHeader: undefined,
        _disableCookies: true,
        _jar: undefined,
        port: 443,
        host: 'www.adidas.co.uk',
        path: '/api/search/taxonomy?query=men',
        httpModule: [Object],
        agentClass: [Function],
        agent: [Agent],
        _started: true,
        href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
     params: null },
  _multipart:
   Multipart {
     request:
      Request {
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        uri: [Url],
        headers: [Object],
        followRedirect: true,
        maxRedirects: 10,
        callback: [Function],
        readable: true,
        writable: true,
        _qs: [Querystring],
        _auth: [Auth],
        _oauth: [OAuth],
        _multipart: [Circular],
        _redirect: [Redirect],
        _tunnel: [Tunnel],
        setHeader: [Function],
        hasHeader: [Function],
        getHeader: [Function],
        removeHeader: [Function],
        method: 'GET',
        localAddress: undefined,
        pool: {},
        dests: [],
        __isRequestRequest: true,
        _callback: [Function],
        proxy: null,
        tunnel: true,
        setHost: true,
        originalCookieHeader: undefined,
        _disableCookies: true,
        _jar: undefined,
        port: 443,
        host: 'www.adidas.co.uk',
        path: '/api/search/taxonomy?query=men',
        httpModule: [Object],
        agentClass: [Function],
        agent: [Agent],
        _started: true,
        href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
     boundary: '54c4df7c-a16d-45e6-a57b-bb7f83e6b1fa',
     chunked: false,
     body: null },
  _redirect:
   Redirect {
     request:
      Request {
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        uri: [Url],
        headers: [Object],
        followRedirect: true,
        maxRedirects: 10,
        callback: [Function],
        readable: true,
        writable: true,
        _qs: [Querystring],
        _auth: [Auth],
        _oauth: [OAuth],
        _multipart: [Multipart],
        _redirect: [Circular],
        _tunnel: [Tunnel],
        setHeader: [Function],
        hasHeader: [Function],
        getHeader: [Function],
        removeHeader: [Function],
        method: 'GET',
        localAddress: undefined,
        pool: {},
        dests: [],
        __isRequestRequest: true,
        _callback: [Function],
        proxy: null,
        tunnel: true,
        setHost: true,
        originalCookieHeader: undefined,
        _disableCookies: true,
        _jar: undefined,
        port: 443,
        host: 'www.adidas.co.uk',
        path: '/api/search/taxonomy?query=men',
        httpModule: [Object],
        agentClass: [Function],
        agent: [Agent],
        _started: true,
        href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
     followRedirect: true,
     followRedirects: true,
     followAllRedirects: false,
     followOriginalHttpMethod: false,
     allowRedirect: [Function],
     maxRedirects: 10,
     redirects: [],
     redirectsFollowed: 0,
     removeRefererHeader: false },
  _tunnel:
   Tunnel {
     request:
      Request {
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        uri: [Url],
        headers: [Object],
        followRedirect: true,
        maxRedirects: 10,
        callback: [Function],
        readable: true,
        writable: true,
        _qs: [Querystring],
        _auth: [Auth],
        _oauth: [OAuth],
        _multipart: [Multipart],
        _redirect: [Redirect],
        _tunnel: [Circular],
        setHeader: [Function],
        hasHeader: [Function],
        getHeader: [Function],
        removeHeader: [Function],
        method: 'GET',
        localAddress: undefined,
        pool: {},
        dests: [],
        __isRequestRequest: true,
        _callback: [Function],
        proxy: null,
        tunnel: true,
        setHost: true,
        originalCookieHeader: undefined,
        _disableCookies: true,
        _jar: undefined,
        port: 443,
        host: 'www.adidas.co.uk',
        path: '/api/search/taxonomy?query=men',
        httpModule: [Object],
        agentClass: [Function],
        agent: [Agent],
        _started: true,
        href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
     proxyHeaderWhiteList:
      [ 'accept',
        'accept-charset',
        'accept-encoding',
        'accept-language',
        'accept-ranges',
        'cache-control',
        'content-encoding',
        'content-language',
        'content-location',
        'content-md5',
        'content-range',
        'content-type',
        'connection',
        'date',
        'expect',
        'max-forwards',
        'pragma',
        'referer',
        'te',
        'user-agent',
        'via' ],
     proxyHeaderExclusiveList: [] },
  setHeader: [Function],
  hasHeader: [Function],
  getHeader: [Function],
  removeHeader: [Function],
  method: 'GET',
  localAddress: undefined,
  pool: {},
  dests: [],
  __isRequestRequest: true,
  _callback: [Function],
  proxy: null,
  tunnel: true,
  setHost: true,
  originalCookieHeader: undefined,
  _disableCookies: true,
  _jar: undefined,
  port: 443,
  host: 'www.adidas.co.uk',
  path: null,
  httpModule:
   { Agent: { [Function: Agent] super_: [Function] },
     globalAgent:
      Agent {
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 443,
        protocol: 'https:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256,
        maxCachedSessions: 100,
        _sessionCache: [Object] },
     Server: { [Function: Server] super_: [Function] },
     createServer: [Function: createServer],
     get: [Function: get],
     request: [Function: request] },
  agentClass:
   { [Function: Agent]
     super_:
      { [Function: Agent] super_: [Function], defaultMaxSockets: Infinity } },
  agent:
   Agent {
     _events: [Object: null prototype] { free: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     defaultPort: 443,
     protocol: 'https:',
     options: { path: null },
     requests: {},
     sockets: { 'www.adidas.co.uk:443::::::::::::::::': [] },
     freeSockets: {},
     keepAliveMsecs: 1000,
     keepAlive: false,
     maxSockets: Infinity,
     maxFreeSockets: 256,
     maxCachedSessions: 100,
     _sessionCache: { map: {}, list: [] } },
  _started: true,
  href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men',
  _agentKey: 'www.adidas.co.uk:443::::::::::::::::' }
HTTP 10384: sockets www.adidas.co.uk:443:::::::::::::::: 1
HTTP 10384: outgoing message end.
HTTP 10384: SOCKET ERROR: read ECONNRESET Error: read ECONNRESET
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27)
handleResponse...
Response error
{ Error: read ECONNRESET
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }
HTTP 10384: outgoing message end.
HTTP 10384: CLIENT socket onClose
HTTP 10384: removeSocket www.adidas.co.uk:443:::::::::::::::: writable: false
HTTP 10384: HTTP socket close
HTTP 10384: server socket close

更新: 它适用于 axios:

app.use('/api', cacheMiddleware(), function (req, out) {
  var url = getDestinationUrl(req);
  if (req.method === 'POST') {
    axios.post(url, req.body)
      .then(response => {
        handleResponse(req, out, response, response.data)
      })
      .catch(error => {
        handleResponseError(req, out, error)
      });
  } else {
    axios.get(url)
      .then(response => {
        handleResponse(req, out, response, response.data)
      })
      .catch(error => {
        handleResponseError(req, out, error)
      });
  }
});

"ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection.

此错误仅表示另一方以可能不正常(或可能很匆忙)的方式关闭了连接。

例如: 套接字连接可能由于多种原因被对方突然关闭,或者您在 运行 您的应用程序时丢失了 wifi 信号。然后你会在你这边看到这个错误/异常。

解决方案- 发生这种情况是因为您没有 listening/handling 到 'error' 事件,要处理它,您应该放置一个可以处理此类事件的侦听器错误。

  • 安全 "throwing" 错误
  • 安全 "catching" 错误

它在 Axios 中工作,因为您正在使用 promises 进行异步错误处理。

除了上述正确答案外,这可能对某些人有所帮助。

我遇到了同样的错误信息。在我的例子中,服务器的 env 变量是 'https://servername' 而不是 'servername'。删除 'https://' 部分修复错误。