Consider this Express app code snippet. What will be the response when a GET request is made to /test?
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log('First middleware');
next();
});
app.get('/test', (req, res, next) => {
res.send('Hello from /test');
next();
});
app.use((req, res) => {
res.send('Fallback middleware');
});
app.listen(3000);Now, if we move the fallback middleware app.use((req, res) => {...}) above the app.get('/test', ...) route, what will happen?
const express = require('express'); const app = express(); app.use((req, res, next) => { console.log('First middleware'); next(); }); // Fallback middleware moved here app.use((req, res) => { res.send('Fallback middleware'); }); app.get('/test', (req, res, next) => { res.send('Hello from /test'); next(); }); app.listen(3000);
Think about how Express processes middleware and routes in the order they are added.
Express runs middleware and routes in the order they are registered. If the fallback middleware is placed before the route, it will catch the request and send a response first. The route handler will never run because the response is already sent.
Given the following Express app setup, which middleware function will run first when a request is made to /api/data?
const express = require('express');
const app = express();
app.use('/api', (req, res, next) => {
console.log('API middleware');
next();
});
app.use((req, res, next) => {
console.log('General middleware');
next();
});
app.get('/api/data', (req, res) => {
res.send('Data response');
});
app.listen(3000);Remember that middleware with path prefixes only run if the request path matches.
Middleware registered with a path prefix like '/api' only runs for matching routes. Since the request is to '/api/data', the '/api' middleware runs first. Then the general middleware runs because it applies to all paths.
Look at this Express app code. Why does it hang and never send a response?
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log('Middleware 1');
// Missing next() call
});
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(3000);Think about how Express moves from one middleware to the next.
Middleware functions must call next() to pass control to the next middleware or route. Since Middleware 1 does not call next() or send a response, the request hangs.
Examine this Express middleware code snippet. What error will it cause?
app.use((req, res, next) => {
if(req.method === 'GET')
next();
else
res.send('Only GET allowed');
next();
});Look carefully at the indentation and flow of next() calls.
The else block sends a response but then next() is called again outside the else block, causing Express to try to continue after response is sent, leading to 'Cannot set headers after they are sent' error.
Which of the following best explains why the order of middleware matters in Express?
Think about how Express passes requests through middleware and routes.
Express processes middleware and routes in the order they are registered. If middleware that sends a response runs before a route, the route never executes. Thus, order controls flow and response behavior.