0
0
Expressframework~5 mins

Router level middleware in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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) =&gt; {<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
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
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
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();
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
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.