Consider this Express.js app with two middleware functions. What will be the console output and response?
const express = require('express'); const app = express(); app.use((req, res, next) => { console.log('First middleware'); next(); }); app.use((req, res, next) => { console.log('Second middleware'); res.send('Hello from second'); }); app.listen(3000);
Middleware runs in the order they are added. The first calls next(), so the second runs next.
Middleware functions run in the order they are added. The first middleware logs and calls next(), so the second middleware runs next and sends the response.
Given this Express.js middleware chain, what is the final value of req.processed when the response is sent?
const express = require('express'); const app = express(); app.use((req, res, next) => { req.processed = 1; next(); }); app.use((req, res, next) => { req.processed += 2; next(); }); app.use((req, res) => { res.send(`Processed: ${req.processed}`); }); app.listen(3000);
Each middleware modifies req.processed and calls next() to continue.
The first middleware sets req.processed = 1. The second adds 2, making it 3. The last sends the value.
Look at this Express.js code. Why does the client never receive a response?
const express = require('express'); const app = express(); app.use((req, res, next) => { console.log('Middleware 1'); // missing next() call }); app.use((req, res) => { res.send('Hello'); }); app.listen(3000);
Middleware must call next() to continue to the next middleware.
The first middleware does not call next(), so the request stops there and no response is sent.
Which of these Express.js middleware declarations will cause a syntax error?
Check the function parameter list syntax carefully.
Option D is missing a comma between res and next, causing a syntax error.
You have three middleware: logger, errorHandler, and router. Which order ensures that errors from router are caught by errorHandler?
Error handling middleware must come after the middleware that might cause errors.
The error handler must be last so it can catch errors from previous middleware like the router.