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'
✗ Incorrect
app.use('/admin', adminRouter) mounts the router so all routes inside adminRouter are prefixed with '/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
✗ Incorrect
The virtual prefix '/user' is added before '/profile', so the full path is '/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
✗ Incorrect
Virtual path prefixes help organize routes by grouping them under a shared base URL.
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
✗ Incorrect
You can mount many routers each with different virtual path prefixes to organize routes.
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
✗ Incorrect
Mounting without a prefix means routes are accessible directly at their defined paths from the root.
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.
Practice
(1/5)
1. What is the main purpose of using a virtual path prefix in Express with app.use('/prefix', router)?
easy
A. To handle errors globally in the app
B. To create a new Express app instance
C. To serve static files from a directory
D. To group routes under a common URL path for better organization
Solution
Step 1: Understand what app.use('/prefix', router) does
This syntax mounts a router or middleware under the URL path '/prefix'.
Step 2: Identify the purpose of grouping routes
Grouping routes under a common prefix helps organize and maintain the app's URL structure.
Final Answer:
To group routes under a common URL path for better organization -> Option D
Quick Check:
Virtual path prefix = route grouping [OK]
Hint: Virtual path prefixes group routes under one URL path [OK]
Common Mistakes:
Thinking it creates a new app instance
Confusing with static file serving
Assuming it handles errors
2. Which of the following is the correct syntax to mount a router on the virtual path prefix '/api' in Express?
easy
A. app.route('/api', router);
B. app.use('/api', router);
C. app.mount('/api', router);
D. app.router('/api', router);
Solution
Step 1: Recall Express method to mount middleware or routers
Express uses app.use(path, middleware) to mount routers or middleware.
Step 2: Check each option's method name
Only app.use is the correct method; others like app.route, app.mount, and app.router are invalid or incorrect here.
Final Answer:
app.use('/api', router); -> Option B
Quick Check:
Mount router = app.use(path, router) [OK]
Hint: Use app.use to mount routers on paths [OK]
Common Mistakes:
Using app.route instead of app.use
Trying non-existent methods like app.mount
Confusing router mounting with route definitions
3. Given the code below, what URL path will respond with 'Hello from admin'?
D. Router should be created with express() not express.Router()
Solution
Step 1: Check the app.use argument order
Correct syntax is app.use(path, router), but here it is reversed.
Step 2: Identify the correct argument order
It should be app.use('/user', userRouter) to mount the router at '/user'.
Final Answer:
The order of arguments in app.use is incorrect -> Option A
Quick Check:
app.use(path, router) order matters [OK]
Hint: app.use(path, router) not app.use(router, path) [OK]
Common Mistakes:
Swapping router and path arguments
Thinking router path is invalid
Forgetting to call app.listen
5. You want to create an Express app with two routers: adminRouter mounted at '/admin' and userRouter mounted at '/user'. Both routers have a route for '/' that sends different greetings. Which code correctly sets this up so visiting '/admin' and '/user' shows the correct messages?
hard
A. app.use('/admin', adminRouter);
app.use('/user', userRouter);
B. app.use(adminRouter, '/admin');
app.use(userRouter, '/user');
C. app.route('/admin', adminRouter);
app.route('/user', userRouter);
D. app.get('/admin', adminRouter);
app.get('/user', userRouter);
Solution
Step 1: Recall correct syntax for mounting routers with prefixes
Use app.use(path, router) to mount routers at virtual path prefixes.
Step 2: Evaluate the options
The correct code uses app.use('/admin', adminRouter); and app.use('/user', userRouter);. Distractors use invalid methods like app.route() or app.get() with routers, or reverse the path and router order in app.use().
Final Answer:
app.use('/admin', adminRouter);
app.use('/user', userRouter); -> Option A
Quick Check:
Mount routers with app.use(path, router) [OK]
Hint: Mount routers with app.use(path, router) for multiple prefixes [OK]
Common Mistakes:
Swapping router and path in app.use
Using app.route or app.get to mount routers
Not mounting routers at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.