Complete the code to define an error-handling middleware function in Express.
app.use(function(err, req, res, [1]) { res.status(500).send('Something broke!'); });
next parameter.The error-handling middleware in Express must have four parameters: err, req, res, and next. The next parameter is required even if unused.
Complete the code to correctly call the next middleware in an error handler.
app.use(function(err, req, res, next) {
if (!err) return [1]();
res.status(500).send('Error occurred');
});res() instead of next().next().next when no error exists.In Express, calling next() passes control to the next middleware. Here, if there is no error, we call next() to continue.
Fix the error in the middleware signature to make it a valid error handler.
app.use(function([1], req, res, next) { res.status(500).send('Oops!'); });
err last instead of first.Error-handling middleware must have four parameters in this order: err, req, res, next. The code had the wrong order and missing err as first parameter.
Fill both blanks to create a middleware that logs errors and passes them on.
app.use(function([1], req, res, [2]) { console.error([1]); [2]([1]); });
res or req instead of err or next.next(err) to pass the error.The error object is err and the function to pass control is next. The middleware logs the error and calls next(err) to continue error handling.
Fill all three blanks to send a JSON error response with status 500.
app.use(function([1], req, [2], [3]) { [2].status(500).json({ error: [1].message }); });
req and res parameters.next parameter.The error object is err, the response object is res, and next is the last parameter. The middleware sends a JSON response with the error message and status 500.