0
0
Node.jsframework~5 mins

Error-handling middleware in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is error-handling middleware in Node.js Express?
It is a special type of middleware designed to catch and handle errors that occur during request processing. It has four parameters: (err, req, res, next).
Click to reveal answer
beginner
How do you define an error-handling middleware function in Express?
By creating a function with four arguments: (err, req, res, next). Express recognizes it as error-handling middleware because of the first 'err' parameter.
Click to reveal answer
intermediate
Why should error-handling middleware be placed after all other middleware and routes?
Because it catches errors from previous middleware or routes. If placed earlier, it won't catch errors from later code.
Click to reveal answer
intermediate
What happens if you call next() with an error inside a middleware?
Express skips normal middleware and routes, then calls the next error-handling middleware to handle the error.
Click to reveal answer
beginner
How can you send a user-friendly error message from error-handling middleware?
Use res.status() to set HTTP status code and res.json() or res.send() to send a clear message explaining the error.
Click to reveal answer
Which of these is the correct signature for error-handling middleware in Express?
A(req, res)
B(err, req, res, next)
C(req, res, next)
D(err, req, res)
Where should error-handling middleware be placed in your Express app?
AOnly inside route handlers
BBefore all routes and middleware
CAnywhere in the code
DAfter all routes and middleware
What does calling next(err) inside a middleware do?
APasses control to the next error-handling middleware
BSends a response immediately
CStops the request processing
DPasses control to the next normal middleware
How can you send a 404 error using error-handling middleware?
Anext(404)
Bres.send(404)
Cres.status(404).send('Not Found')
Dthrow 404
What parameter identifies a middleware as error-handling middleware?
AThe presence of 'err' as the first parameter
BThe use of res.send()
CThe use of next()
DThe absence of next parameter
Explain how error-handling middleware works in Express and why its signature is important.
Think about how Express knows which middleware handles errors.
You got /4 concepts.
    Describe the steps to create and use error-handling middleware in a Node.js Express app.
    Focus on function signature, placement, and usage.
    You got /4 concepts.