title: ECONNRESET in Express.js (Node.js) with multiple requests
body: <p>Given a standard Express.js setup</p>
  
  <pre><code>const express = require('express');
  const app = express();
  const router = express.Router();
  
  router.get('/test/:id', (req, res) =&gt; {
    return res.status(200).json({ hello: 'world' });
  });
  
  app.use('/api', router);
  
  app.listen(3000, () =&gt; console.info('Up on port 3000));
  </code></pre>
  
  <p>I am making 1000 requests agains the endpoint, one after the other:</p>
  
  <pre><code>const fetch = require('node-fetch');
  for (let i = 0; i &lt; 1000; i++) {
    let id = Math.floor(Math.random() * 12) + 1;
    fetch(`http://localhost:3000/api/test/${id}`).then(res =&gt; res.json()).then(data =&gt; console.log(data)).catch(error =&gt; console.error(error));
  }
  </code></pre>
  
  <p>I do see the data returned however, every now and then I see an <code>ECONNRESET</code> error. The amount of <code>ECONNRESET</code> error messages also vary: sometimes I get a few, sometimes a lot more. I do understand the message but I can't get my head around solving the issue behind it.</p>
  
  <p>Here's a sample error:</p>
  
  <pre><code>{ FetchError: request to http://localhost:3000/api/test/8 failed, reason: connect ECONNRESET 127.0.0.1:3000
      at ClientRequest.&lt;anonymous&gt; (node_modules/node-fetch/lib/index.js:1345:11)
      at ClientRequest.emit (events.js:182:13)
      at Socket.socketErrorListener (_http_client.js:399:9)
      at Socket.emit (events.js:182:13)
      at emitErrorNT (internal/streams/destroy.js:82:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
      at process.internalTickCallback (internal/process/next_tick.js:72:19)
    message:
     'request to http://localhost:3000/api/departments/8 failed, reason: connect ECONNRESET 127.0.0.1:3000',
    type: 'system',
    errno: 'ECONNRESET',
    code: 'ECONNRESET' }
  </code></pre>
  
  <p>Note that I have tried to make the request using axios, the built-in HTTP module all to avail. I'm sure the issue is with my Express app handling the request but not sure how to fix it exactly.</p>
  
  <p>Update 1:</p>
  
  <p>As per the suggestion in the comment, here's the async version:</p>
  
  <pre><code>async function f() {
    const array = Array.from(Array(1000).keys());
    for (const el of array) {
      try {
        let id = Math.floor(Math.random() * 12) + 1;
        const result = await fetch(`http://localhost:3000/api/test/${id}`).then(res =&gt; res.json());
        console.log(result);
        return result;
      } catch(e) {
        console.log(e);
      }
    }
  }
  
  f();
  </code></pre>
  
  <p>Now I am receiving occasional <code>ECONNREFUSED</code> messages.</p>
  
  <p>Update 2:</p>
  
  <p>Based on  Mazki516's answer here's the solution that works:</p>
  
  <pre><code>// previous require statements
  const cluster = require('cluster');
  const os = require('os');
  
  if (cluster.isMaster) {
    const cpuCount = os.cpus().length
    for (let i = 0; i &lt; cpuCount; i++) {
        cluster.fork()
    }
  } else {
    const app = express();
    // rest of the route definitions
    // also app.listen() etc...
  }
  cluster.on('exit', worker =&gt; {
    console.log(`${worker.id} removed`);
    cluster.fork();
  });
  </code></pre>