NodeJs ECONNRESET

会有一股神秘感。 提交于 2020-03-18 11:19:09

问题


I get an error when I'm executing GET request.

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

It happens when I try to make GET request to https://www.adidas.co.uk/api/search/taxonomy?query=men but it works when I use https://jsonplaceholder.typicode.com/api/todos/1.

Request:

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) });
      }

    });

log:

    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

UPDATE: It works with 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)
      });
  }
});

回答1:


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

This error simply means that the other side closed the connection in a way that was probably not normal (or may be in a hurry).

An example is: a socket connection can be closed by the other party abruptly due to several reasons or you lost your wifi signal while running your app. Then you will see this error / exception on your side.

Solution- It happening because you are not listening/handling to the 'error' event, to deal with it you should put a listener which can handle such errors.

  • Safely "throwing" errors
  • Safely "catching" errors

Its working in Axios because you are using promises for async error handling.



来源:https://stackoverflow.com/questions/53912439/nodejs-econnreset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!