Recall & Review
beginner
What is the special signature of an error-handling middleware function in Express?
An error-handling middleware function in Express has four parameters:
(err, req, res, next). The first parameter err is the error object, followed by the usual request, response, and next function.Click to reveal answer
intermediate
Why does Express require error-handling middleware to have four parameters?
Express uses the number of parameters to identify error-handling middleware. If a middleware has four parameters, Express treats it as an error handler and calls it only when an error occurs.
Click to reveal answer
beginner
Show a simple example of an Express error-handling middleware function.
Example:<br><pre>function errorHandler(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
}</pre>Click to reveal answer
intermediate
What happens if you define an error-handling middleware with only three parameters in Express?
Express will not recognize it as an error handler and will treat it as a normal middleware. This means it won't be called automatically when an error occurs.
Click to reveal answer
beginner
How do you pass an error to the error-handling middleware in Express?
You call
next(err) inside a middleware or route handler, passing the error object. Express then skips normal middleware and calls the error-handling middleware.Click to reveal answer
What is the correct signature for an Express error-handling middleware?
✗ Incorrect
Error-handling middleware must have four parameters: err, req, res, and next.
How does Express know a middleware is for error handling?
✗ Incorrect
Express checks if the middleware has four parameters to identify it as an error handler.
What happens if you call next() with an error inside a route handler?
✗ Incorrect
Calling next(err) tells Express to skip normal middleware and call error handlers.
Which of these is NOT true about error-handling middleware in Express?
✗ Incorrect
Error-handling middleware is called only when an error is passed to next().
If an error-handling middleware is missing, what happens when an error occurs?
✗ Incorrect
Express has a default error handler that sends a generic error response if no custom handler is defined.
Explain the signature of an Express error-handling middleware and why it is important.
Think about how Express knows which middleware handles errors.
You got /3 concepts.
Describe how to trigger an error-handling middleware from a route or normal middleware in Express.
Focus on the role of the next function.
You got /3 concepts.