0
0
Expressframework

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

Choose your learning style9 modes available
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?
A(req, res, next)
B(err, req, res, next)
C(req, res)
D(err, req, res)
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)
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
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
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
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.