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?
AOn an Express router instance using router.use()
BDirectly on the Express app using app.use()
CInside route handler functions only
DIn the package.json file
✗ 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()?
AThe request will hang and not proceed to the next handler
BThe request will automatically continue
CThe server will restart
DThe middleware will be skipped
✗ 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?
AIt cannot modify the request object
BIt only runs for routes defined in its router
CIt runs for every route in the app
DIt must be defined after all routes
✗ 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?
Aconst router = Router();
Bconst router = new Router();
Cconst router = express.createRouter();
Dconst router = express.Router();
✗ 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?
ATo configure the Express app settings
BTo serve static files globally
CTo add authentication checks for routes in a specific router
DTo connect to the database
✗ 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.
Practice
(1/5)
1. What is the main purpose of router-level middleware in Express?
easy
A. To replace the main app middleware completely
B. To run middleware only for routes defined in a specific router
C. To handle errors globally across all routers
D. To serve static files from a directory
Solution
Step 1: Understand router-level middleware scope
Router-level middleware runs only for routes defined in that router, not the entire app.
Step 2: Compare with other middleware types
Main app middleware runs for all routes, error middleware handles errors, static middleware serves files. Router middleware is specific to router routes.
Final Answer:
To run middleware only for routes defined in a specific router -> Option B
Quick Check:
Router middleware scope = specific router routes [OK]
Hint: Router middleware affects only its router's routes [OK]
Common Mistakes:
Confusing router middleware with app-level middleware
A. The last middleware does not call next(), causing requests to hang
B. The last middleware is missing next() and will cause 404 for all routes
C. The last middleware will never run because it is after a route handler
D. The last middleware should have 4 parameters to handle errors
Solution
Step 1: Analyze middleware execution order
Middleware and routes execute in the order defined. First middleware runs for all requests and calls next().
Step 2: Trace unmatched route behavior
For unmatched routes like /foo: first middleware calls next(), /hello route doesn't match so next(), last middleware runs but does nothing--no response and no next() call.
Step 3: Identify the consequence
The request pipeline stops without a response, causing the client request to hang indefinitely.
Final Answer:
The last middleware does not call next(), causing requests to hang -> Option A
Quick Check:
No response + no next() = request hangs [OK]
Hint: Every middleware must either respond or call next() [OK]
Common Mistakes:
Thinking empty catch-all middleware is harmless
Forgetting unmatched routes reach the last middleware
Assuming the router automatically handles 404s
5. You want to apply a logging middleware only to routes under /api in your Express app. You create a router and add middleware like this: