Challenge - 5 Problems
Virtual Path Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing a virtual path?
Consider this Express setup using a virtual path prefix. What will be the response when a client requests
/api/users?Express
const express = require('express'); const app = express(); const router = express.Router(); router.get('/users', (req, res) => { res.send('User list'); }); app.use('/api', router); app.listen(3000);
Attempts:
2 left
💡 Hint
Remember that the router is mounted on '/api', so routes inside the router are relative to that prefix.
✗ Incorrect
The router handles requests starting with '/api'. The route '/users' inside the router matches '/api/users' on the main app, so the response is 'User list'.
❓ state_output
intermediate2:00remaining
What is the value of req.baseUrl inside a virtual path router?
Given this Express code, what will
req.baseUrl be when the route handler runs for a request to /admin/dashboard?Express
const express = require('express'); const app = express(); const adminRouter = express.Router(); adminRouter.get('/dashboard', (req, res) => { res.send(req.baseUrl); }); app.use('/admin', adminRouter); app.listen(3000);
Attempts:
2 left
💡 Hint
req.baseUrl shows the path where the router is mounted.✗ Incorrect
req.baseUrl contains the path prefix where the router is mounted, which is '/admin'.📝 Syntax
advanced2:00remaining
Which option correctly mounts a router with a virtual path prefix?
Select the code snippet that correctly mounts a router on the path '/api/v1' so that routes inside the router are prefixed accordingly.
Attempts:
2 left
💡 Hint
The
app.use method takes the path prefix first, then the router.✗ Incorrect
The correct syntax is
app.use('/api/v1', router);. Other options are invalid syntax or incorrect usage.🔧 Debug
advanced2:00remaining
Why does this virtual path router not respond as expected?
Given the code below, why does a request to
/api/products return 404 Not Found?Express
const express = require('express'); const app = express(); const router = express.Router(); router.get('products', (req, res) => { res.send('Product list'); }); app.use('/api', router); app.listen(3000);
Attempts:
2 left
💡 Hint
Check the route path string inside router.get.
✗ Incorrect
Route paths must start with a slash. Without it, the route does not match requests correctly.
🧠 Conceptual
expert3:00remaining
How does Express handle nested virtual path prefixes?
If you mount a router
routerA at /api and inside it mount another router routerB at /v2, what is the full path to access a route /items defined inside routerB?Express
const express = require('express'); const app = express(); const routerA = express.Router(); const routerB = express.Router(); routerB.get('/items', (req, res) => { res.send('Items v2'); }); routerA.use('/v2', routerB); app.use('/api', routerA); app.listen(3000);
Attempts:
2 left
💡 Hint
Think about how mounting routers adds path prefixes in order.
✗ Incorrect
Mounting routerB at '/v2' inside routerA mounted at '/api' creates the combined path '/api/v2'. Adding '/items' gives '/api/v2/items'.