Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the output of this custom middleware logging request method and URL?
Consider this Express.js middleware that logs the HTTP method and URL of incoming requests. What will be printed to the console when a GET request is made to '/home'?
Node.js
function logRequest(req, res, next) {
console.log(`${req.method} ${req.url}`);
next();
}
// Simulated request object
const req = { method: 'GET', url: '/home' };
const res = {};
logRequest(req, res, () => console.log('Next middleware called'));Attempts:
2 left
💡 Hint
Look at the order of console.log statements and the use of next()
✗ Incorrect
The middleware logs the method and URL first, then calls next(), which triggers the next console.log. So the output is the method and URL line, then 'Next middleware called'.
❓ state_output
intermediate1:30remaining
What is the value of res.locals.user after this middleware runs?
Given this middleware that sets a user object on res.locals, what will be the value of res.locals.user after it executes?
Node.js
function setUser(req, res, next) {
res.locals.user = { id: 42, name: 'Alice' };
next();
}
const res = { locals: {} };
setUser({}, res, () => {});
console.log(res.locals.user);Attempts:
2 left
💡 Hint
res.locals is used to pass data between middleware and routes
✗ Incorrect
The middleware assigns an object with id and name to res.locals.user, so after running, res.locals.user holds that object.
📝 Syntax
advanced2:00remaining
Which option correctly defines error-handling middleware in Express?
Express error-handling middleware must have a specific function signature. Which of these options correctly defines such middleware?
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.
🔧 Debug
advanced2:00remaining
Why does this custom middleware cause the server to hang?
This middleware logs the request but the server never responds or moves on. What is the cause?
Node.js
function logAndForget(req, res, next) {
console.log('Request received');
// missing next() call
}
// Usage in Express app
// app.use(logAndForget);Attempts:
2 left
💡 Hint
Middleware must call next() or end the response
✗ Incorrect
Without calling next() or sending a response, Express waits forever, causing the server to hang.
🧠 Conceptual
expert2:30remaining
Which statement best describes the order of middleware execution in Express?
Given multiple middleware functions registered in an Express app, how does Express decide the order in which to run them?
Attempts:
2 left
💡 Hint
Think about how Express processes middleware functions sequentially
✗ Incorrect
Express executes middleware in the order they are registered. Each middleware must call next() to continue the chain; otherwise, the chain stops.