express

Error handling

Syntax#

  • app.use(function(err, req, res, next) {}) // Basic middleware

Parameters#

Name Description
err Object with error information
req HTTP request object
res HTTP response object
next function used to start next middleware execution
## Basic sample
Unlike other middleware functions error-handling middleware functions have four arguments instead of three: (err, req, res, next).

Sample:

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Error found!');
});

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow