2

I am using nodemon npm package for automatically restarting the node application when file changes in the directory are detected. Server is created by using express and http npm package. The problem is when server is idle for few minutes it throws error Error: read ECONNRESET actual error is as follows:

events.js:167
      throw er; // Unhandled 'error' event
      ^
Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
    at Connection._handleProtocolError (/home/abc/node_modules/mysql/lib/Connection.js:425:8)
    at Protocol.emit (events.js:182:13)
    at Protocol._delegateError (/home/abc/node_modules/mysql/lib/protocol/Protocol.js:390:10)
    at Protocol.handleNetworkError (/home/abc/node_modules/mysql/lib/protocol/Protocol.js:363:10)
    at Connection._handleNetworkError (/home/abc/node_modules/mysql/lib/Connection.js:420:18)
    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._tickCallback (internal/process/next_tick.js:63:19)

I have tried to catch the error but this does not solve my problem.

process.on('uncaughtException', function(err) {  
   console.log("uncaughtException called: \n", err);
})

The server is created by using express and http:

express = require('express');
app = express();
http = require('http');
var httpServer = http.createServer(app);
httpServer.listen(3000, function(req, res) {
    console.log('Server running at port 3000);
});

I expect to prevent the server from ECONNRESET error occurrence What's the solution?

Atharva Jawalkar
  • 166
  • 1
  • 12

1 Answers1

1

We can see that this is an mysql problem based on the error thrown:

Emitted 'error' event at:
at Connection._handleProtocolError (/home/abc/node_modules/mysql/lib/Connection.js:425:8)

I was getting the same problem, and the way that I fixed it was after making a connection to the database I had added a setInterval of every 20 minutes performing a simple query such as db.query('SELECT 1;'). Supposedly mysql closes the connection after some idle time so based on this post

"The module developers recommended using a heartbeat to keep the connection alive such as calling SELECT 1; on an interval."

This is a quick fix, and it worked for me but if you want to figure out a different way I recommend reading the post above as there are other solutions to it.