Namespaces help organize routes in Express by grouping related paths together. This keeps your code clean and easy to manage.
Namespaces for separation in Express
Introduction
Syntax
Express
const express = require('express'); const app = express(); const router = express.Router(); // Define routes on router router.get('/path', (req, res) => { res.send('response'); }); // Use router with a namespace app.use('/namespace', router);
The express.Router() creates a mini app for routes.
The app.use('/namespace', router) adds a prefix to all routes in the router.
Examples
/user/profile route by adding the /user namespace.Express
const userRouter = express.Router(); userRouter.get('/profile', (req, res) => { res.send('User Profile'); }); app.use('/user', userRouter);
/admin/dashboard route separate from other routes.Express
const adminRouter = express.Router(); adminRouter.get('/dashboard', (req, res) => { res.send('Admin Dashboard'); }); app.use('/admin', adminRouter);
Sample Program
This Express app uses two namespaces: /user and /admin. Each has its own router with routes inside. This keeps user and admin routes separate and organized.
Express
const express = require('express'); const app = express(); const userRouter = express.Router(); userRouter.get('/profile', (req, res) => { res.send('User Profile Page'); }); const adminRouter = express.Router(); adminRouter.get('/dashboard', (req, res) => { res.send('Admin Dashboard Page'); }); app.use('/user', userRouter); app.use('/admin', adminRouter); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Important Notes
Namespaces help avoid route conflicts by adding a clear prefix.
You can nest routers inside other routers for deeper organization.
Remember to use app.use() to connect routers with namespaces.
Summary
Namespaces group related routes under a common path prefix.
Use express.Router() to create route groups.
Attach routers to the main app with app.use('/namespace', router).
Practice
1. What is the main purpose of using namespaces in Express routing?
easy
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 AQuick 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
Solution
Step 1: Recall Express method to use middleware or routers
Express usesapp.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 DQuick 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?
const express = require('express');
const app = express();
const userRouter = express.Router();
userRouter.get('/profile', (req, res) => res.send('User Profile'));
app.use('/user', userRouter);medium
Solution
Step 1: Identify the namespace prefix
The router is attached withapp.use('/user', userRouter), so '/user' is the base path.Step 2: Combine router path with namespace
The route inside the router is '/profile', so full path is '/user/profile'.Final Answer:
/user/profile -> Option AQuick Check:
Namespace + route = full path [OK]
Hint: Full path = namespace + router path [OK]
Common Mistakes:
- Ignoring the namespace prefix
- Using only the router path without prefix
- Mixing order of path segments
4. Identify the error in the following Express code that tries to create a namespace:
const express = require('express');
const app = express();
const adminRouter = express.Router();
adminRouter.get('/dashboard', (req, res) => res.send('Admin Dashboard'));
app.use(adminRouter, '/admin');medium
Solution
Step 1: Check app.use syntax
The correct syntax isapp.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 CQuick 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?
hard
Solution
Step 1: Create routers for each namespace
BothapiRouterandadminRouterare created usingexpress.Router().Step 2: Attach routers with correct syntax
Useapp.use('/path', router)to mount routers under '/api' and '/admin'.Step 3: Verify other options
Options A, B, and D use incorrect methods or argument orders.Final Answer:
const apiRouter = express.Router(); const adminRouter = express.Router(); app.use('/api', apiRouter); app.use('/admin', adminRouter); -> Option BQuick Check:
Separate namespaces need separate routers and app.use() [OK]
Hint: Use separate routers and app.use('/path', router) [OK]
Common Mistakes:
- Reversing arguments in app.use
- Using app.route or app.attach incorrectly
- Trying to attach routers without namespaces
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
