📖 Scenario: You are building a simple Express server that handles user and product routes separately.To keep the code organized, you want to use route prefixing so that all user routes start with /users and all product routes start with /products.
🎯 Goal: Create an Express app with two routers: one for users and one for products. Use route prefixing to mount the routers so that user routes are under /users and product routes are under /products.
📋 What You'll Learn
Create an Express app instance called app
Create two routers called userRouter and productRouter
Add a GET route / to userRouter that sends 'User list'
Add a GET route / to productRouter that sends 'Product list'
Mount userRouter on the /users path using route prefixing
Mount productRouter on the /products path using route prefixing
💡 Why This Matters
🌍 Real World
Route prefixing helps organize server code by grouping related routes under a common path, making the code easier to maintain and scale.
💼 Career
Understanding route prefixing is essential for backend developers working with Express to build clean and modular APIs.
Progress0 / 4 steps
1
DATA SETUP: Create Express app and routers
Create an Express app instance called app and two routers called userRouter and productRouter using express.Router().
Express
Hint
Use const app = express() to create the app.
Use express.Router() to create routers.
2
CONFIGURATION: Add GET routes to routers
Add a GET route / to userRouter that sends 'User list' and a GET route / to productRouter that sends 'Product list'.
Express
Hint
Use router.get('/', (req, res) => { res.send('text') }) to add routes.
3
CORE LOGIC: Mount routers with route prefixing
Mount userRouter on the /users path and productRouter on the /products path using app.use().
Express
Hint
Use app.use('/prefix', router) to mount routers with prefixes.
4
COMPLETION: Start the Express server
Add code to start the Express server on port 3000 using app.listen().
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What is the main purpose of route prefixing in Express?
easy
A. To encrypt route URLs for security
B. To speed up the server response time
C. To automatically generate HTML pages
D. To group routes under a shared path for better organization
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 D
Quick Check:
Route prefixing = Group routes under shared path [OK]
Hint: Route prefixing groups routes by shared path prefix [OK]
Common Mistakes:
Thinking it improves server speed
Confusing with automatic HTML generation
Assuming it encrypts URLs
2. Which of the following is the correct way to prefix routes using Express Router?
easy
A. app.use('/api', router);
B. router.prefix('/api');
C. app.route('/api').use(router);
D. router.attach('/api');
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 A
Quick Check:
Prefix routes with app.use(prefix, router) [OK]
Hint: Use app.use('/prefix', router) to prefix routes [OK]
Common Mistakes:
Using non-existent router.prefix() method
Trying app.route() to prefix router
Using router.attach() which doesn't exist
3. Given this code, what URL path will respond with 'Hello World'?