Challenge - 5 Problems
Express Middleware Master
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 the following Express app code. What will the client receive when making a GET request to '/'?
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { req.customValue = 'Hello'; next(); }); app.use((req, res, next) => { req.customValue += ' World'; next(); }); app.get('/', (req, res) => { res.send(req.customValue); });
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object before the route handler.
✗ Incorrect
The first middleware adds 'Hello' to req.customValue. The second appends ' World'. The route sends the combined string.
❓ lifecycle
intermediate2:00remaining
In what order are these middlewares executed?
Given this Express app setup, what is the order of console logs when a GET request to '/' is made?
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { console.log('First middleware'); next(); }); app.get('/', (req, res, next) => { console.log('Route handler'); next(); }); app.use((req, res) => { console.log('Last middleware'); res.send('Done'); });
Attempts:
2 left
💡 Hint
Remember that app.use middlewares run in the order they are added.
✗ Incorrect
Middleware and route handlers run in the order they are registered. The last app.use acts as a catch-all after the route.
🔧 Debug
advanced2:00remaining
Why does this cause a 'headers already sent' error?
Examine this Express app. Why does the server throw an error when making a GET request to '/stop'?
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { if (req.path === '/stop') { res.send('Stopped here'); return; } next(); }); app.get('/stop', (req, res) => { res.send('This will never run'); });
Attempts:
2 left
💡 Hint
Think about what happens when next() is called after sending a response.
✗ Incorrect
Calling next() after res.send() causes Express to continue processing, leading to errors because headers are already sent.
📝 Syntax
advanced2:00remaining
Which middleware definition is syntactically correct?
Identify the correct syntax for an Express application-level middleware function.
Attempts:
2 left
💡 Hint
Middleware functions must have three parameters and call next() properly if not ending response.
✗ Incorrect
Option D correctly defines middleware with three parameters and sends a response. Option D misses next parameter usage. Option D calls next without parentheses. Option D misses semicolon causing syntax error.
❓ state_output
expert3:00remaining
What is the final value of req.processed after all middleware run?
Given this Express app, what is the value of req.processed when the client requests '/'?
Express
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.get('/', (req, res) => { req.processed *= 3; res.send(req.processed.toString()); });
Attempts:
2 left
💡 Hint
Track the changes to req.processed step by step through middleware and route.
✗ Incorrect
First middleware sets req.processed = 1. Second adds 2 making it 3. Route multiplies by 3 resulting in 9.