How does error-handling middleware work in Express?
Model answer
It is a function with four parameters (err, req, res, next), registered after your routes. When a route calls next(err) or throws, Express skips normal middleware and runs it. There you log the error and send a consistent response with the right status code, without leaking stack traces in production.
Learn this topic
Express Middleware Explained: next(), Logging and express.json() in the course Node.js and Express: Build a Back End API from Scratch.
Sign in to add your own answer.