0
0
Expressframework~10 mins

Error-handling middleware signature in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an error-handling middleware function in Express.

Express
app.use(function(err, req, res, [1]) {
  res.status(500).send('Something broke!');
});
Drag options to blanks, or click blank then click option'
Arequest
Bres
Cerror
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using only three parameters instead of four.
Naming the last parameter incorrectly.
Omitting the next parameter.
2fill in blank
medium

Complete the code to correctly call the next middleware in an error handler.

Express
app.use(function(err, req, res, next) {
  if (!err) return [1]();
  res.status(500).send('Error occurred');
});
Drag options to blanks, or click blank then click option'
Ares
Bnext
Cerr
Dreq
Attempts:
3 left
💡 Hint
Common Mistakes
Calling res() instead of next().
Forgetting to call next().
Passing the error to next when no error exists.
3fill in blank
hard

Fix the error in the middleware signature to make it a valid error handler.

Express
app.use(function([1], req, res, next) {
  res.status(500).send('Oops!');
});
Drag options to blanks, or click blank then click option'
Aerr
Brequest
Cnext
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Placing err last instead of first.
Using only three parameters.
Misnaming parameters.
4fill in blank
hard

Fill both blanks to create a middleware that logs errors and passes them on.

Express
app.use(function([1], req, res, [2]) {
  console.error([1]);
  [2]([1]);
});
Drag options to blanks, or click blank then click option'
Aerr
Bnext
Cres
Dreq
Attempts:
3 left
💡 Hint
Common Mistakes
Using res or req instead of err or next.
Not calling next(err) to pass the error.
5fill in blank
hard

Fill all three blanks to send a JSON error response with status 500.

Express
app.use(function([1], req, [2], [3]) {
  [2].status(500).json({ error: [1].message });
});
Drag options to blanks, or click blank then click option'
Aerr
Bres
Cnext
Dreq
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up req and res parameters.
Omitting the next parameter.
Not sending a JSON response.