What if you could change many routes' base path by editing just one line?
Why Route prefixing in Express? - Purpose & Use Cases
Imagine building a web app with many pages like /users, /users/profile, /users/settings, and you have to write each full path manually for every route.
Manually typing full paths for every route is tiring, easy to make mistakes, and hard to update if the base path changes. It's like rewriting the same address over and over.
Route prefixing lets you set a common starting path once, so all related routes automatically share it. This keeps your code clean and easy to maintain.
app.get('/users/profile', handler); app.get('/users/settings', handler);
const userRouter = express.Router(); userRouter.get('/profile', handler); userRouter.get('/settings', handler); app.use('/users', userRouter);
It enables organizing routes logically and updating base paths quickly without hunting through all your code.
Think of a mailing address: instead of writing the city and street every time, you write it once and just add the house number for each letter.
Manually writing full route paths is repetitive and error-prone.
Route prefixing groups related routes under a shared base path.
This makes your code cleaner and easier to update.
Practice
Solution
Step 1: Understand route prefixing concept
Route prefixing means putting multiple routes under one common path prefix to keep code organized.Step 2: Identify the main benefit
This grouping helps keep URLs clean and code easier to maintain by avoiding repetition of the same path.Final Answer:
To group routes under a shared path for better organization -> Option DQuick Check:
Route prefixing = Group routes under shared path [OK]
- Thinking it improves server speed
- Confusing with automatic HTML generation
- Assuming it encrypts URLs
Solution
Step 1: Recall Express Router usage
Express uses app.use(prefix, router) to attach a router with a path prefix.Step 2: Check each option
Only app.use('/api', router) is valid syntax; others are not Express methods.Final Answer:
app.use('/api', router); -> Option AQuick Check:
Prefix routes with app.use(prefix, router) [OK]
- Using non-existent router.prefix() method
- Trying app.route() to prefix router
- Using router.attach() which doesn't exist
const express = require('express');
const app = express();
const router = express.Router();
router.get('/hello', (req, res) => {
res.send('Hello World');
});
app.use('/api', router);
Solution
Step 1: Understand route prefixing effect
The router defines '/hello', but it is attached to app with prefix '/api'.Step 2: Combine prefix and route path
The full path becomes '/api' + '/hello' = '/api/hello'.Final Answer:
/api/hello -> Option CQuick Check:
Prefix + route path = full URL [OK]
- Ignoring the prefix and choosing '/hello'
- Choosing just the prefix '/api' without route
- Reversing prefix and route order
const express = require('express');
const app = express();
const router = express.Router();
router.get('/test', (req, res) => {
res.send('Test route');
});
app.use(router, '/api');Solution
Step 1: Check app.use argument order
app.use expects (path, router), but code uses (router, path) which is wrong.Step 2: Confirm other parts are correct
router.get with '/test' is valid; express.Router() is correct; res.send works fine.Final Answer:
The order of arguments in app.use is incorrect -> Option BQuick Check:
app.use(path, router) order matters [OK]
- Swapping arguments in app.use
- Thinking router path can't start with '/'
- Confusing express.Router() with express.Route()
Solution
Step 1: Understand correct mounting syntax
To prefix routers, use app.use(prefix, router) for each router separately.Step 2: Evaluate options
app.use('/users', usersRouter); app.use('/products', productsRouter); correctly uses app.use with prefix and router. Others misuse methods or argument order.Final Answer:
app.use('/users', usersRouter); app.use('/products', productsRouter); -> Option AQuick Check:
Mount each router with app.use(prefix, router) [OK]
- Using router.use() without attaching to app
- Using app.route() incorrectly for routers
- Swapping arguments in app.use
Ready to prove it?
Pass the test and this pattern moves unknown
