# Error-Handling - Express
Handling errors (opens new window)
Errors are handled by one or more special middleware functions that have four arguments, instead of the usual three: (err, req, res, next)
. For example:
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
These can return any content required, but must be called after all other app.use()
and routes calls so that they are the last middleware in the request handling process!
Note: HTTP404 and other "error" status codes are not treated as errors. If you want to handle these, you can add a middleware function to do so. For more information see the FAQ (opens new window).
For more information see Error handling (opens new window) (Express docs).