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.
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?
A(req, res, next)
B(err, req, res, next)
C(req, res)
D(err, req, res)
✗ Incorrect
Error-handling middleware must have four parameters: err, req, res, and next.
How does Express know a middleware is for error handling?
ABy a special flag in options
BBy the middleware name
CBy the order it is declared
DBy the number of parameters (4)
✗ 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?
AExpress skips normal middleware and calls error-handling middleware
BExpress ignores the error and continues
CExpress crashes immediately
DExpress sends a default error response automatically
✗ 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?
AIt can be called without an error
BIt can send a response to the client
CIt is called only when an error occurs
DIt must have four parameters
✗ 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?
AThe error is ignored
BThe app crashes
CExpress sends a default error response
DThe request hangs forever
✗ 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.
Practice
(1/5)
1. Which of the following correctly describes the signature of an error-handling middleware function in Express?
easy
A. It has five parameters: err, req, res, next, done.
B. It has three parameters: req, res, next.
C. It has two parameters: req, res.
D. It has four parameters: err, req, res, next.
Solution
Step 1: Recall Express middleware function signatures
Normal middleware functions have three parameters: req, res, next. Error-handling middleware must have four parameters.
Step 2: Identify the error-handling middleware signature
Error-handling middleware functions always start with err as the first parameter, followed by req, res, next.
Final Answer:
It has four parameters: err, req, res, next. -> Option D
Quick Check:
Error middleware signature = err, req, res, next [OK]
Hint: Error middleware always has 4 parameters starting with err [OK]
Common Mistakes:
Using only three parameters for error middleware
Forgetting the err parameter at the start
Adding extra parameters beyond four
2. Which of the following is the correct syntax to define an error-handling middleware in Express?
easy
A. app.use(function(req, res, next, err) { /* code */ });
B. app.use(function(err, req, res, next) { /* code */ });
C. app.use(function(err, req, next, res) { /* code */ });
D. app.use(function(req, err, res, next) { /* code */ });
Solution
Step 1: Check parameter order in error middleware
The correct order is err, req, res, next. This order is required by Express to recognize error middleware.
Step 2: Match the correct syntax
app.use(function(err, req, res, next) { /* code */ }); matches the correct order and syntax for error-handling middleware.
C. The parameters are in the wrong order; err must be first.
D. res.status(500) is invalid inside error middleware.
Solution
Step 1: Check parameter order for error middleware
Error-handling middleware must have parameters in order: err, req, res, next. Here, err is last, which is incorrect.
Step 2: Understand impact of wrong parameter order
Express will not recognize this as error middleware, so errors won't be caught properly.
Final Answer:
The parameters are in the wrong order; err must be first. -> Option C
Quick Check:
err parameter must be first in error middleware [OK]
Hint: Error middleware must start with err parameter [OK]
Common Mistakes:
Placing err parameter anywhere but first
Assuming next() call is mandatory in all error middleware
Confusing error middleware with normal middleware signature
5. You want to create an error-handling middleware that logs the error and sends a JSON response with status 500. Which of the following is the best correct implementation?
Error middleware must have exactly four parameters in order: err, req, res, next. app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); matches this.
Step 2: Check response status and format
app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); sets status 500 and sends JSON with error message, which is correct and user-friendly.