📖 Scenario: You are building a simple web server using Express.js. You want to organize your routes into separate namespaces to keep your code clean and easy to manage.
🎯 Goal: Create an Express server with two separate namespaces: /api/users and /api/products. Each namespace should respond with a simple JSON message.
📋 What You'll Learn
Create an Express app instance
Create two routers: usersRouter and productsRouter
Mount usersRouter on /api/users
Mount productsRouter on /api/products
Each router should have a GET route on / that sends a JSON response
💡 Why This Matters
🌍 Real World
Organizing routes into namespaces helps keep large Express applications clean and maintainable by grouping related routes together.
💼 Career
Understanding how to use routers and namespaces in Express is essential for backend developers building scalable web APIs.
Progress0 / 4 steps
1
Create the Express app and users router
Create an Express app by calling express() and assign it to app. Then create a router by calling express.Router() and assign it to usersRouter.
Express
Hint
Use const app = express() to create the app and const usersRouter = express.Router() to create the router.
2
Create the products router
Create another router by calling express.Router() and assign it to productsRouter.
Express
Hint
Create productsRouter the same way as usersRouter.
3
Add GET routes to both routers
Add a GET route on / to usersRouter that sends JSON { message: 'Users namespace' }. Also add a GET route on / to productsRouter that sends JSON { message: 'Products namespace' }.
Express
Hint
Use router.get('/', (req, res) => { res.json(...) }) for each router.
4
Mount routers on namespaces and start server
Mount usersRouter on /api/users and productsRouter on /api/products using app.use(). Then start the server on port 3000 using app.listen().
Express
Hint
Use app.use('/api/users', usersRouter) and app.use('/api/products', productsRouter). Then start the server with app.listen(3000).
Practice
(1/5)
1. What is the main purpose of using namespaces in Express routing?
easy
A. To group related routes under a common path prefix
B. To improve database query speed
C. To style HTML elements
D. To manage user sessions
Solution
Step 1: Understand what namespaces do in Express
Namespaces help organize routes by grouping them under a shared path prefix.
Step 2: Compare with other options
Options B, C, and D relate to other concerns like database, styling, or sessions, not routing namespaces.
Final Answer:
To group related routes under a common path prefix -> Option A
Quick Check:
Namespaces = Group routes [OK]
Hint: Namespaces group routes by path prefix [OK]
Common Mistakes:
Confusing namespaces with database or styling concepts
Thinking namespaces manage sessions
Believing namespaces affect frontend styling
2. Which of the following is the correct way to attach a router to an Express app under the namespace '/api'?
easy
A. app.router('/api', router);
B. app.attach('/api', router);
C. app.route('/api', router);
D. app.use('/api', router);
Solution
Step 1: Recall Express method to use middleware or routers
Express uses app.use(path, router) to mount routers under a path.
Step 2: Check other options for validity
Options A, B, and C are not valid Express methods for attaching routers.
Final Answer:
app.use('/api', router); -> Option D
Quick Check:
Attach router with app.use() [OK]
Hint: Use app.use('/path', router) to attach routers [OK]
Common Mistakes:
Using app.router instead of app.use
Confusing app.route with router attachment
Trying to use non-existent methods like app.attach
3. Given the code below, what will be the full URL path to access the route handler?
C. Router should be attached with app.use('/admin', adminRouter);
D. Cannot use express.Router() for namespaces
Solution
Step 1: Check app.use syntax
The correct syntax is app.use('/path', router), but here arguments are reversed.
Step 2: Confirm other options
adminRouter.get should be adminRouter.post is incorrect because GET is valid; C is unrelated to namespace error; D is false because express.Router() is used for namespaces.
Final Answer:
Router should be attached with app.use('/admin', adminRouter); -> Option C
Quick Check:
app.use(path, router) order matters [OK]
Hint: app.use(path, router) order is important [OK]
Common Mistakes:
Swapping arguments in app.use
Thinking HTTP method must be POST
Confusing missing listen with routing error
5. You want to create two separate namespaces in Express: '/api' for API routes and '/admin' for admin routes. Which code snippet correctly sets this up?