Challenge - 5 Problems
Express Advanced Patterns 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 this Express app code. What will the client receive when requesting '/'?
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { req.customValue = 5; next(); }); app.use((req, res, next) => { req.customValue += 10; next(); }); app.get('/', (req, res) => { res.send(`Value is ${req.customValue}`); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object in order.
✗ Incorrect
Each middleware adds or modifies req.customValue before the final handler sends it. The first sets it to 5, the second adds 10, so the final value is 15.
📝 Syntax
intermediate2:00remaining
Which option correctly defines an Express error-handling middleware?
Express error-handling middleware must have a specific function signature. Which option below is correct?
Attempts:
2 left
💡 Hint
Error middleware must have four parameters: err, req, res, next.
✗ Incorrect
Express recognizes error middleware by the presence of four parameters. Only option A has all four parameters correctly.
🔧 Debug
advanced2:00remaining
Why does this Express route handler never send a response?
Look at this code snippet. Why does the client hang without receiving a response?
Express
app.get('/data', (req, res) => { fetchDataFromDB((err, data) => { if (err) { res.status(500).send('DB error'); } }); });
Attempts:
2 left
💡 Hint
Check what happens when there is no error in the callback.
✗ Incorrect
If there is no error, the callback does not send any response, so the client waits forever.
🧠 Conceptual
advanced2:00remaining
Why use the 'next' function in Express middleware?
What is the main purpose of calling next() inside Express middleware functions?
Attempts:
2 left
💡 Hint
Think about how Express moves through middleware layers.
✗ Incorrect
Calling next() tells Express to continue to the next middleware or route handler. Without it, the request would hang.
❓ state_output
expert2:00remaining
What is the final value of 'count' after these Express middleware run?
Given this Express app code, what is the value of count when the client receives the response?
Express
const express = require('express'); const app = express(); let count = 0; app.use((req, res, next) => { count += 1; next(); }); app.use((req, res, next) => { count += 2; next(); }); app.get('/', (req, res) => { count += 3; res.send(`Count is ${count}`); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Add the increments from each middleware and the route handler.
✗ Incorrect
The first middleware adds 1, the second adds 2, and the route handler adds 3, totaling 6.