title: How to handle the ECONNRESET error in nodejs?
body: <p>I'm using nodejs as a backend and postgres as my database.It works fine for few minutes after starting the server.But,after few minutes it get's crashed and resulting the following error,</p>
  <pre><code>events.js:292
        throw er; // Unhandled 'error' event
        ^
  
  Error: read ECONNRESET
      at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
  Emitted 'error' event on Client instance at:
      at Connection.connectedErrorHandler (D:\panda_new_1\fishfarmiot\node_modules\pg\lib\client.js:221:10)
      at Connection.emit (events.js:315:20)
      at Socket.reportStreamError (D:\panda_new_1\fishfarmiot\node_modules\pg\lib\connection.js:57:10)
      at Socket.emit (events.js:315:20)
      at emitErrorNT (internal/streams/destroy.js:92:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
      at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    errno: 'ECONNRESET',
    code: 'ECONNRESET',
    syscall: 'read'
  }
  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! app@1.0.0 start: `node ./bin/www`
  npm ERR! Exit status 1
  npm ERR!
  npm ERR! Failed at the app@1.0.0 start script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  
  npm ERR! A complete log of this run can be found in:
  npm ERR!     C:\Users\Welcome\AppData\Roaming\npm-cache\_logs\2020-09-01T11_23_58_697Z-debug.log
  </code></pre>
  <p>My connectionstring is,</p>
  <pre><code>var config = {
      db: {
        
          connectionString: 'postgresql://localhost'
      },
  </code></pre>
  <p>My code is,</p>
  <pre><code>let connectionString = config.db.connectionString;
  const client = new pg.Client(connectionString);
  client.connect();
  
  router.post('/addAlert', (req, res) =&gt; {
      var sql = &quot;SELECT public.alert_insert('[&quot; + JSON.stringify(req.body) + &quot;]')&quot;
      client.query(sql)
          .then(users =&gt; res.status(200).json({ message: &quot;Created Successfully&quot;, status: 200 }))
          .catch(err =&gt; res.status(500).json({ message: &quot;SomeThing Went Wrong&quot;, status: 500 }))
  }
  )
  router.put('/editAlert', (req, res) =&gt; {
      var sql = &quot;SELECT * FROM alert_edit('&quot; + JSON.stringify(req.body) + &quot;'::jsonb)&quot;
      client.query(sql)
          .then(users =&gt; res.status(200).json({ message: &quot;Updated Successfully&quot;, status: 200 }))
          .catch(err =&gt; res.status(404).json({ message: &quot;Id Not Found&quot;, status: 404 }))
  }
  )
  router.post('/getAll', (req, res) =&gt; {
      var sql = &quot;SELECT * FROM alert_getall('&quot; + JSON.stringify(req.body) + &quot;'::jsonb);&quot;
      client.query(sql)
          .then(users =&gt; res.status(200).send(users.rows))
          .catch(err =&gt; res.status(400).send(err))
  }
  )
  router.delete('/deleteAlert', (req, res) =&gt; {
      var sql = &quot;SELECT * FROM alert_delete('&quot; + JSON.stringify(req.body) + &quot;'::jsonb);&quot;
      client.query(sql)
          .then(users =&gt; res.status(200).json({ message: &quot;Deleted Successfully&quot;, status: 200 }))
          .catch(err =&gt; res.status(404).son({ message: &quot;Id Not Found&quot;, status: 404 }))
  }
  )
  module.exports = router
  </code></pre>
  <p>can anyone suggest me, how to handle this error?</p>