0
0
Expressframework~5 mins

Error-handling middleware in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is error-handling middleware in Express?
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)
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
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
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
Which parameter in error-handling middleware holds the error object?
Anext
Bres
Creq
Derr
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.