Question
What is the purpose of Express.js's built-in error handling middleware?
Asked by: USER6679
71 Viewed
71 Answers
Answer (71)
Express's built-in error handling middleware (defined using `app.use((err, req, res, next) => { ... })`) is the last resort for handling errors that haven't been caught by any previous middleware. It receives four arguments: `err`, `req`, `res`, and `next`. It's crucial for preventing unhandled exceptions from crashing the server and providing a default error response.