0
0
Expressframework~20 mins

Router level middleware in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Router Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 ?
AThe response will be empty because the middleware does not call next().
BThe response will be undefined because router middleware does not modify req object.
CThe response will be an error because middleware cannot set properties on req.
DThe response will be 'hello' because the router middleware sets req.customValue before the route handler.
Attempts:
2 left
💡 Hint
Router middleware runs before route handlers inside that router and can modify req and res objects.
📝 Syntax
intermediate
2: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');
});
Arouter.use(logMethod);
Bapp.use('/router', logMethod);
Crouter.get(logMethod);
Dapp.use(logMethod);
Attempts:
2 left
💡 Hint
Router-level middleware is applied with router.use() before defining routes.
🔧 Debug
advanced
2: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);
AMiddleware is missing the next parameter.
BMiddleware must call res.send() to run.
CMiddleware is defined after the route, so it never runs for matched routes.
DMiddleware must be defined on app, not router.
Attempts:
2 left
💡 Hint
Order of middleware and routes matters in Express.
state_output
advanced
2: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)); });
A'3'
B'1'
C'2'
Dundefined
Attempts:
2 left
💡 Hint
Middleware runs in order and can modify req object cumulatively.
🧠 Conceptual
expert
2:00remaining
Which statement about router-level middleware is true?
Select the correct statement about router-level middleware in Express.
ARouter-level middleware runs for every request to the app regardless of the path.
BRouter-level middleware runs only if the request path matches the router's mount path and the middleware is defined before the route handlers.
CRouter-level middleware cannot modify the request or response objects.
DRouter-level middleware must always end the response and cannot call next().
Attempts:
2 left
💡 Hint
Think about when router middleware runs and how it interacts with routes.