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 chain?
Consider this Express.js middleware setup. What will the server respond with when a GET request is made to '/'?
Node.js
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); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Think about how each middleware modifies the request object before the final handler.
✗ Incorrect
The first middleware sets req.message to 'Hello'. The second appends ' World'. The final handler sends the combined string.
❓ lifecycle
intermediate2:00remaining
Which middleware runs first in this Express app?
Given this Express app, which middleware function executes first when a GET request to '/test' is received?
Node.js
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('/test', (req, res, next) => { console.log('Handler 1'); next(); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Remember that app.use middlewares run before route handlers unless path or method restricts them.
✗ Incorrect
app.use middlewares run before route handlers. Middleware 1 logs first, then Middleware 2, then the handler.
📝 Syntax
advanced2:00remaining
What error does this middleware code produce?
Examine this middleware function. What error will occur when a request is made?
Node.js
app.use((req, res, next) => {
res.send('Hello');
next();
});
app.use((req, res, next) => {
res.send('World');
});Attempts:
2 left
💡 Hint
Think about what happens if you call next() after sending a response.
✗ Incorrect
Calling next() after res.send() causes Express to try to send another response, which is not allowed.
🔧 Debug
advanced2:00remaining
Why does this middleware not modify the request as expected?
This middleware is supposed to add a user property to req, but it doesn't work. Why?
Node.js
app.use('/api', (req, res, next) => { req.user = { id: 1 }; // missing next call }); app.get('/api/data', (req, res) => { res.json(req.user); });
Attempts:
2 left
💡 Hint
Middleware must call next() to continue the chain.
✗ Incorrect
Without calling next(), Express does not proceed to the next middleware or route handler, so req.user is never sent.
🧠 Conceptual
expert3:00remaining
What is the correct order of middleware execution in Express?
Arrange these middleware and route handler steps in the order Express executes them for a matching request.
Attempts:
2 left
💡 Hint
Global middleware runs before route-specific middleware and handlers. Error middleware runs last if needed.
✗ Incorrect
Express runs global middleware first, then route-specific middleware, then the route handler. Error middleware runs only if an error is passed.