Error handling

Other topics

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!');
});

Syntax:

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

Parameters:

NameDescription
errObject with error information
reqHTTP request object
resHTTP response object
nextfunction used to start next middleware execution

Contributors

Topic Id: 2739

Example Ids: 9216

This site is not affiliated with any of the contributors.