Complete the code to define an error-handling middleware in Express.
app.use(function(err, req, res, [1], next) { res.status(500).send('Something broke!'); });
The error-handling middleware in Express must have four parameters: err, req, res, and next. The next parameter is required to identify it as error-handling middleware.
Complete the code to send a JSON error response with status 400.
app.use(function(err, req, res, next) {
res.status([1]).json({ error: err.message });
});To indicate a client error, the status code 400 is used. This sends a JSON response with the error message.
Fix the error in the middleware signature to correctly handle errors.
app.use(function(req, res, next, [1]) { res.status(500).send('Error occurred'); });
The error-handling middleware must have the error parameter err as the first argument, followed by req, res, and next. The order matters.
Fill both blanks to correctly pass the error to the next middleware.
app.use(function(err, req, res, [1]) { if (err) { [2](err); } else { next(); } });
res or send instead of next.next with the error.The next function is used to pass control to the next middleware. Calling next(err) passes the error along the middleware chain.
Fill all three blanks to create a middleware that logs the error and sends a 500 response.
app.use(function([1], req, res, [2]) { console.error([3]); res.status(500).send('Server error'); });
The error parameter is named err and the next middleware function is next. Logging the error uses console.error(err).