0
0
Expressframework~5 mins

Virtual path prefixes in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a virtual path prefix in Express?
A virtual path prefix is a path segment added before the actual route path when mounting middleware or routers. It helps organize routes under a common base URL without repeating the prefix in each route.
Click to reveal answer
beginner
How do you use a virtual path prefix with an Express router?
You use app.use('/prefix', router) to mount the router so all routes inside the router are accessed under '/prefix'. For example, if router has '/users', the full path becomes '/prefix/users'.
Click to reveal answer
beginner
Why use virtual path prefixes instead of writing full paths in each route?
Virtual path prefixes keep code cleaner and easier to maintain by grouping related routes under one base path. It avoids repeating the same prefix in every route definition.
Click to reveal answer
beginner
Example: What URL path will respond if you mount a router with app.use('/api', router) and router has router.get('/items')?
The URL path '/api/items' will respond because '/api' is the virtual prefix and '/items' is the route inside the router.
Click to reveal answer
intermediate
Can virtual path prefixes be nested in Express?
Yes, you can nest routers with their own prefixes. For example, app.use('/api', apiRouter) and inside apiRouter you use apiRouter.use('/v1', v1Router). The full path becomes '/api/v1/...'.
Click to reveal answer
What does app.use('/admin', adminRouter) do in Express?
AMounts adminRouter so all its routes start with '/admin'
BChanges all routes to '/admin' only
CRemoves '/admin' from all routes
DCreates a new server on '/admin'
If router has router.get('/profile'), and you mount it with app.use('/user', router), what URL path responds?
A/profile
B/profile/user
C/user
D/user/profile
Why are virtual path prefixes useful?
AThey speed up server response
BThey organize routes under a common base path
CThey encrypt URLs
DThey disable routes
Can you mount multiple routers with different virtual prefixes in Express?
ANo, only one router per app
BOnly if routers have no routes
CYes, each with its own prefix
DOnly if prefixes are the same
What happens if you mount a router without a virtual path prefix like app.use(router)?
ARoutes are mounted at root '/'
BRoutes are disabled
CRoutes get random prefixes
DServer crashes
Explain what a virtual path prefix is in Express and why it is helpful.
Think about grouping routes under one common URL segment.
You got /3 concepts.
    Describe how you would use virtual path prefixes to organize routes for an API with versions like /api/v1 and /api/v2.
    Consider nesting routers with app.use calls.
    You got /3 concepts.