Recall & Review
beginner
What is router-level middleware in Express?
Router-level middleware is a function that runs during the request-response cycle but is bound to an Express router instance. It helps organize middleware for specific routes.
Click to reveal answer
beginner
How do you apply middleware only to routes in a specific router?
You attach middleware to the router instance using
router.use() or directly in route handlers. This middleware runs only for routes handled by that router.Click to reveal answer
beginner
Example: What does this code do?<br><pre>const router = express.Router();<br>router.use((req, res, next) => {<br> console.log('Router middleware');<br> next();<br>});</pre>This middleware logs 'Router middleware' for every request handled by this router, then passes control to the next middleware or route handler.
Click to reveal answer
intermediate
Can router-level middleware modify the request or response objects?
Yes, router-level middleware can change
req or res objects, like adding properties or headers, affecting downstream handlers.Click to reveal answer
intermediate
What is the difference between application-level and router-level middleware?
Application-level middleware applies to all routes in the app, while router-level middleware applies only to routes defined in a specific router instance.
Click to reveal answer
Where do you attach router-level middleware in Express?
✗ Incorrect
Router-level middleware is attached to a router instance using router.use(), so it only affects routes in that router.
What happens if router-level middleware does not call next()?
✗ Incorrect
Middleware must call next() to pass control; otherwise, the request stops and the client waits indefinitely.
Which of these is true about router-level middleware?
✗ Incorrect
Router-level middleware is scoped to the router it is attached to, so it only runs for routes in that router.
How do you create a router in Express to use router-level middleware?
✗ Incorrect
The correct way is to call express.Router() to create a new router instance.
What is a common use case for router-level middleware?
✗ Incorrect
Router-level middleware is often used to add checks like authentication only for routes in that router.
Explain how router-level middleware works in Express and how it differs from application-level middleware.
Think about where you attach the middleware and which routes it affects.
You got /4 concepts.
Describe a scenario where using router-level middleware is helpful and how you would implement it.
Consider grouping routes that share a common need.
You got /4 concepts.