0
0
Expressframework~20 mins

Virtual path prefixes in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Virtual Path Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
AThe server responds with 404 Not Found.
BThe server responds with 'Cannot GET /users'.
CThe server responds with 'User list'.
DThe server crashes with an error.
Attempts:
2 left
💡 Hint
Remember that the router is mounted on '/api', so routes inside the router are relative to that prefix.
state_output
intermediate
2: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);
A"/admin"
B"/dashboard"
C"/admin/dashboard"
D"/"
Attempts:
2 left
💡 Hint
req.baseUrl shows the path where the router is mounted.
📝 Syntax
advanced
2: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.
Aapp.use(router, '/api/v1');
Bapp.use('/api/v1', router);
Capp.use('/api/v1' => router);
Dapp.use(router('/api/v1'));
Attempts:
2 left
💡 Hint
The app.use method takes the path prefix first, then the router.
🔧 Debug
advanced
2: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);
AThe router.get method requires a second argument to be an array.
BThe router is not mounted correctly; it should be app.use(router, '/api').
CThe app.listen call is missing a callback function.
DThe route path is missing a leading slash; it should be '/products'.
Attempts:
2 left
💡 Hint
Check the route path string inside router.get.
🧠 Conceptual
expert
3: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);
A/api/v2/items
B/v2/api/items
C/api/items/v2
D/items/api/v2
Attempts:
2 left
💡 Hint
Think about how mounting routers adds path prefixes in order.