Error-handling middleware is a special type of middleware in Express that catches and processes errors occurring in the app. It has four parameters: (err, req, res, next).
Click to reveal answer
beginner
How do you define an error-handling middleware function in Express?
You define it with four parameters: (err, req, res, next). For example: function (err, req, res, next) { /* handle error */ }.
Click to reveal answer
intermediate
Why must error-handling middleware have four parameters?
Express uses the number of parameters to identify error-handling middleware. Having four parameters tells Express this middleware handles errors.
Click to reveal answer
intermediate
Where should error-handling middleware be placed in the middleware stack?
It should be placed after all other middleware and routes, so it can catch errors from them.
Click to reveal answer
beginner
What happens if you call next() with an error inside a route handler?
Calling next(err) passes the error to the next error-handling middleware, skipping normal middleware.
Click to reveal answer
Which of the following is the correct signature for an Express error-handling middleware?
A(err, req, res, next)
B(req, res, next)
C(req, res)
D(err, req, res)
✗ Incorrect
Error-handling middleware must have four parameters: err, req, res, and next.
Where should you place error-handling middleware in your Express app?
ABefore all routes
BIn the middle of route definitions
CAfter all routes and other middleware
DIt does not matter
✗ Incorrect
Error-handling middleware should be last to catch errors from all previous middleware and routes.
What does calling next(err) inside a route handler do?
ASkips to the next error-handling middleware
BEnds the request without response
CSkips to the next normal middleware
DRestarts the request
✗ Incorrect
Calling next with an error passes control to the next error-handling middleware.
If an error occurs in a route and you don't have error-handling middleware, what happens?
AThe request hangs and never responds
BExpress automatically handles the error and sends a response
CThe error crashes the server
DThe error is ignored
✗ Incorrect
Express has a default error handler that sends a generic error response if no custom error middleware is defined.
Which parameter in error-handling middleware holds the error object?
Anext
Bres
Creq
Derr
✗ Incorrect
The first parameter, err, contains the error passed to the middleware.
Explain how error-handling middleware works in Express and why it needs four parameters.
Think about how Express knows which middleware handles errors.
You got /4 concepts.
Describe the steps to add custom error handling to an Express app.
Consider where and how you write the error middleware function.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of error-handling middleware in Express?
easy
A. To serve static files like images and CSS
B. To parse incoming JSON request bodies
C. To catch errors and prevent the server from crashing
D. To log every request made to the server
Solution
Step 1: Understand middleware roles
Middleware can serve many roles, but error-handling middleware specifically catches errors.
Step 1: Check parameter order for error middleware
Error middleware must have parameters in order: err, req, res, next.
Step 2: Identify incorrect order in code
The code has (req, res, next, err) which is incorrect.
Final Answer:
The parameters are in the wrong order -> Option B
Quick Check:
Error middleware params order = err, req, res, next [OK]
Hint: Error middleware params start with err, not req [OK]
Common Mistakes:
Swapping err and req parameters
Placing error middleware before routes
Forgetting to call next() when needed
5. You want to create an error-handling middleware that logs the error message and then passes the error to the next middleware. Which of the following implementations is correct?
B. app.use((req, res, next, err) => {
console.log(err.message);
next();
});
C. app.use((err, req, res) => {
console.log(err.message);
next(err);
});
D. app.use((err, req, res, next) => {
console.log(err.message);
next(err);
});
Solution
Step 1: Recall requirements for error-handling middleware that chains
It must have four parameters starting with err, log err.message, and call next(err) to pass the error to the next handler.
Step 2: Match against provided implementations
Implementations with incorrect parameter order or count fail to receive or handle err properly; those that respond instead of calling next(err) break the chain.