How to make sure NodeJS recovers after bad requests?

Viewed 698

I was running a NodeJS app with Express 4, Mongoose, and MongoDB. Then I got a possibly bot GET request to which the response was 404. Usually the app carries on after that no problem, but this time it looked like it froze or stalled. And no page would load in my browser although the app was still technically running. It came back to normal after restarting.

After testproxy.php it would just emit half empty lines.

I know there is forever module that would restart your app if it crashes, not sure it would've been helpful in this case.

enter image description here

EDIT:

In my app.js (I was in dev mode):

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

It should've printed 500 Error, but it didn't because it thought it handled the 404 error and I don't think next(err) was ever fired in this case.

2 Answers
Related