0
0
Expressframework~30 mins

Route prefixing in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Route prefixing
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use app.listen(3000) to start the server on port 3000.