1

I am building an applications using Node.js, Express and Mongoose. I am using Cloud9 for a development environment. I used express generator to create the base of my app and have been adding onto it.

I seemingly randomly get this error and the server stops. It doesn't happen during any specific event, it happens in between page loads. It doesn't seem to happen to any specific pages.

I get this error all the time, and I'm constantly having to start it again.

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at exports._errnoException (util.js:870:11)
    at TCP.onread (net.js:552:26)

MongoDB does not crash when I get this error. I just have to start the node server again and it works fine. Until it randomly crashes again.

I'm not sure if this error is just a side-effect of C9, if so I'm not worried about it, I just don't want it to happen in production, especially since I can't see the logs nearly as easily.

If I really am failing to "handle the error" I'd like to know where I'm supposed to do that, I would much prefer my server to not crash when it gets a problem.

stackers
  • 2,701
  • 4
  • 34
  • 66

2 Answers2

2

In node, whenever an 'error' event is emitted and no one listens to it, it will throw.
To make it not throw, put a listener on it and handle it yourself. That way you can log the error with more information.

You can also try using the useful node.js option --abort-on-uncaught-exception. Not only it provides much more verbose and useful error stack trace, but also saves core file on application crash allowing further debug.

To have one listener for a group of calls you can use domains and also catch other errors on runtime. Make sure each async operation related to http(Server/Client) is in different domain context comparing to the other parts of the code, the domain will automatically listen to the error events and will propagate it to it's own handler. So you only listen to that handler and get the error data. You also get more information for free here.

See also this SO answer...

Community
  • 1
  • 1
MarcoS
  • 17,323
  • 24
  • 96
  • 174
1

You can use pm2 for starting your server. Then your server will not crash if there is any error. It will just throw the error and continue to run. It also other useful features as well.

You can read more about pm2 here https://www.npmjs.com/package/pm2

Akshay Kumar
  • 576
  • 2
  • 10
  • Thank you, that's essentially all I needed to know. I was planning to use PM2 anyway in my production environment, so I think I should be all set. – stackers Dec 14 '16 at 19:58