Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express middleware sequence?
Consider this Express app code with three middleware functions. What will be sent to the client when a GET request is made to '/'?
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { req.message = 'Hello'; next(); }); app.use((req, res, next) => { req.message += ' World'; next(); }); app.get('/', (req, res) => { res.send(req.message); });
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object and calls next() to pass control.
✗ Incorrect
The first middleware sets req.message to 'Hello'. The second appends ' World'. The final route sends the combined string.
❓ lifecycle
intermediate2:00remaining
Which middleware runs last before the response is sent?
Given this Express middleware setup, which middleware function executes immediately before the response is sent to the client?
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { console.log('Middleware 1'); next(); }); app.use((req, res, next) => { console.log('Middleware 2'); next(); }); app.get('/', (req, res) => { console.log('Route handler'); res.send('Done'); });
Attempts:
2 left
💡 Hint
Remember the order middleware and route handlers run in Express.
✗ Incorrect
Middleware 1 and 2 run first, then the route handler sends the response. The route handler is last before sending.
🔧 Debug
advanced2:00remaining
Why does this middleware never call the next middleware?
Examine this middleware. Why does the next middleware not run after this one?
Express
app.use((req, res, next) => {
if (!req.user) {
res.status(401).send('Unauthorized');
} else {
next();
}
});
app.use((req, res, next) => {
console.log('Second middleware');
next();
});Attempts:
2 left
💡 Hint
Think about what happens when res.send() is called without next().
✗ Incorrect
When res.send() is called, the response ends. The middleware does not call next() in that case, so the next middleware is skipped.
📝 Syntax
advanced2:00remaining
What error does this middleware code produce?
Identify the error caused by this middleware function code.
Express
app.use((req, res, next) => {
console.log('Start');
next();
console.log('End');
});Attempts:
2 left
💡 Hint
Look carefully at how next is used.
✗ Incorrect
The code calls next without parentheses, so next is treated as an expression without invocation, causing a SyntaxError.
❓ state_output
expert3:00remaining
What is the final value of req.count after this middleware chain?
Given this middleware chain, what is the value of req.count when the response is sent?
Express
app.use((req, res, next) => {
req.count = 1;
next();
});
app.use((req, res, next) => {
req.count += 2;
next();
});
app.use((req, res, next) => {
req.count *= 3;
next();
});
app.get('/', (req, res) => {
res.send(req.count.toString());
});Attempts:
2 left
💡 Hint
Calculate step-by-step how req.count changes in each middleware.
✗ Incorrect
Initial req.count is 1. Then add 2 → 3. Then multiply by 3 → 9. The response sends '9'.