0
0
Expressframework~20 mins

Route prefixing in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Prefixing 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 prefixed routes?
Consider this Express app code with route prefixing. What response will the client receive when requesting /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 client receives an empty response with status 200.
BThe client receives a 404 Not Found error.
CThe client receives 'Cannot GET /users' error.
DThe client receives 'User list' as the response.
Attempts:
2 left
💡 Hint
Remember the router is mounted on '/api', so routes inside it are prefixed.
📝 Syntax
intermediate
2:00remaining
Which option correctly prefixes routes using Express Router?
You want to prefix all routes in a router with '/admin'. Which code snippet correctly achieves this?
Arouter.prefix('/admin'); app.use(router);
Brouter.use('/admin'); app.use(router);
Capp.use('/admin', router);
Dapp.use(router, '/admin');
Attempts:
2 left
💡 Hint
Check how Express mounts routers with prefixes.
🔧 Debug
advanced
2:00remaining
Why does this prefixed route not respond as expected?
Given this code, why does a GET request to '/api/products' return 404?
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 route path is missing a leading slash; it should be '/products'.
BThe router is not mounted correctly on the app.
CThe app.listen call is missing a callback function.
DThe router.get method is used incorrectly; it should be router.post.
Attempts:
2 left
💡 Hint
Check the route path string syntax inside router.get.
state_output
advanced
2:00remaining
What is the output after multiple prefixed routers?
Consider this Express app code:
const express = require('express');
const app = express();
const router1 = express.Router();
const router2 = express.Router();

router2.get('/info', (req, res) => {
  res.send('Info page');
});

router1.use('/details', router2);
app.use('/api', router1);

app.listen(3000);
What response will a GET request to /api/details/info return?
AThe client receives a 404 Not Found error.
BThe client receives 'Info page' as the response.
CThe client receives 'Cannot GET /api/details/info' error.
DThe client receives an empty response with status 200.
Attempts:
2 left
💡 Hint
Routers can be nested and prefixes combine.
🧠 Conceptual
expert
2:00remaining
What is the effect of mounting the same router with different prefixes?
If you mount the same Express router instance twice with different prefixes like this:
const router = express.Router();
router.get('/status', (req, res) => res.send('OK'));

app.use('/service1', router);
app.use('/service2', router);
What happens when a client requests /service2/status?
AThe client receives 'OK' because the router handles requests under both prefixes.
BThe server throws an error at startup due to duplicate router mounting.
CThe client receives a 404 Not Found error because a router cannot be mounted twice.
DThe client receives 'OK' only if the request is to '/service1/status'.
Attempts:
2 left
💡 Hint
Think about how Express routers work when mounted multiple times.