app.use((req, res) => {
res.status(404).send('Page not found');
});This middleware sets the HTTP status to 404 and sends the text 'Page not found'. So the client gets a 404 status with that message.
Option A has correct syntax with balanced braces and parentheses. Others have missing or misplaced characters causing syntax errors.
app.get('/hello', (req, res) => { res.send('Hello world'); }); app.use((req, res) => { res.status(404).send('Not found'); }); app.use((err, req, res, next) => { res.status(500).send('Server error'); });
The route '/hello' matches requests to '/hello' and sends a response, so the request never reaches the 404 handler middleware.
app.use((req, res, next) => {
res.status(404);
next();
});
app.use((req, res) => {
res.send('Page missing');
});The first middleware sets status 404 and calls next(), so the next middleware sends the body with status 404 preserved.
404 handlers catch unmatched routes and must be last. Error handlers have four parameters; 404 handlers usually have three. Express does not auto-send 404 responses. 404 handlers can send any content type.
