Challenge - 5 Problems
Router Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does router-level middleware affect request handling?
Consider an Express app with a router that has middleware attached. What happens when a request matches a route inside that router?
Express
const express = require('express'); const app = express(); const router = express.Router(); router.use((req, res, next) => { req.customValue = 'hello'; next(); }); router.get('/test', (req, res) => { res.send(req.customValue); }); app.use('/api', router); // What will be the response when a client requests GET /api/test ?
Attempts:
2 left
💡 Hint
Router middleware runs before route handlers inside that router and can modify req and res objects.
✗ Incorrect
Router-level middleware runs for all routes defined on that router. It can modify the request object, and since it calls next(), the route handler receives the modified req object and can use the new property.
📝 Syntax
intermediate2:00remaining
Identify the correct syntax to apply router-level middleware
Which option correctly applies middleware only to routes inside a router in Express?
Express
const express = require('express'); const router = express.Router(); // Middleware to log request method function logMethod(req, res, next) { console.log(req.method); next(); } // Apply middleware here router.get('/hello', (req, res) => { res.send('Hi'); });
Attempts:
2 left
💡 Hint
Router-level middleware is applied with router.use() before defining routes.
✗ Incorrect
Using router.use(logMethod) applies the middleware to all routes inside that router. app.use applies middleware globally or to a path on the app, not just the router.
🔧 Debug
advanced2:00remaining
Why does this router-level middleware not run?
Given this code, why does the middleware not log anything when requesting /api/data?
const express = require('express');
const app = express();
const router = express.Router();
router.get('/data', (req, res) => {
res.send('Data');
});
router.use((req, res, next) => {
console.log('Middleware ran');
next();
});
app.use('/api', router);
Attempts:
2 left
💡 Hint
Order of middleware and routes matters in Express.
✗ Incorrect
Express runs middleware and routes in the order they are defined. Since the route is defined before the middleware, the route matches and sends a response before middleware runs.
❓ state_output
advanced2:00remaining
What is the value of req.processed after these middlewares?
Analyze the following router-level middleware sequence:
const express = require('express');
const router = express.Router();
router.use((req, res, next) => {
req.processed = 1;
next();
});
router.use((req, res, next) => {
req.processed += 2;
next();
});
router.get('/check', (req, res) => {
res.send(String(req.processed));
});
Attempts:
2 left
💡 Hint
Middleware runs in order and can modify req object cumulatively.
✗ Incorrect
The first middleware sets req.processed to 1, the second adds 2, so the final value is 3 sent as a string.
🧠 Conceptual
expert2:00remaining
Which statement about router-level middleware is true?
Select the correct statement about router-level middleware in Express.
Attempts:
2 left
💡 Hint
Think about when router middleware runs and how it interacts with routes.
✗ Incorrect
Router-level middleware runs only when the request path matches the router's mount path and is defined before route handlers to affect them. It can modify req/res and must call next() to continue.