Mounting routers with app.use helps organize your web app by grouping related routes together. It keeps your code clean and easier to manage.
Mounting routers with app.use in Express
const express = require('express'); const app = express(); const router = express.Router(); // Define routes on the router router.get('/path', (req, res) => { res.send('response'); }); // Mount the router on a path app.use('/basepath', router);
app.use mounts the router at the specified base path.
Routes inside the router will be relative to the base path.
userRouter at /users. Visiting /users shows 'User home'.const express = require('express'); const app = express(); const userRouter = express.Router(); userRouter.get('/', (req, res) => { res.send('User home'); }); app.use('/users', userRouter);
productRouter at /products. Visiting /products/list shows 'Product list'.const express = require('express'); const app = express(); const productRouter = express.Router(); productRouter.get('/list', (req, res) => { res.send('Product list'); }); app.use('/products', productRouter);
This example creates a blogRouter with two routes: '/' and '/post'. It mounts the router at '/blog'. So, visiting '/blog' shows 'Welcome to the blog home' and '/blog/post' shows 'This is a blog post'.
const express = require('express'); const app = express(); const port = 3000; // Create a router for blog routes const blogRouter = express.Router(); // Define a route on blogRouter blogRouter.get('/', (req, res) => { res.send('Welcome to the blog home'); }); blogRouter.get('/post', (req, res) => { res.send('This is a blog post'); }); // Mount blogRouter at /blog app.use('/blog', blogRouter); // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Remember that the paths inside the router are relative to the mount path.
You can mount multiple routers on different paths to keep your app organized.
Use app.use to add middleware or routers to your app.
Mounting routers with app.use helps organize routes by grouping them under a base path.
It makes your code cleaner and easier to maintain.
Routes inside routers are relative to the path you mount them on.
Practice
app.use to mount routers in Express?Solution
Step 1: Understand what
app.usedoesapp.usemounts middleware or routers at a specified path, grouping routes.Step 2: Identify the benefit of mounting routers
Mounting routers organizes routes under a base path, making code cleaner and easier to maintain.Final Answer:
To group related routes under a common base path for better organization -> Option CQuick Check:
Mount routers = group routes [OK]
- Thinking app.use replaces the app
- Believing it sends responses directly
- Confusing mounting with automatic page generation
userRouter on the path /users using app.use?Solution
Step 1: Recall the
The correct syntax isapp.usesyntaxapp.use(path, router)to mount a router at a path.Step 2: Match the correct order and method
app.use('/users', userRouter); usesapp.use('/users', userRouter);which is correct. Others have wrong order or method.Final Answer:
app.use('/users', userRouter); -> Option AQuick Check:
app.use(path, router) = correct syntax [OK]
- Swapping router and path order
- Using app.get instead of app.use
- Using non-existent app.mount method
const express = require('express');
const app = express();
const userRouter = express.Router();
userRouter.get('/', (req, res) => res.send('User list'));
app.use('/users', userRouter);
Solution
Step 1: Understand router path and mounting
The router defines a GET route at '/' which is relative to where it is mounted.Step 2: Combine mount path and router route
SinceuserRouteris mounted at '/users', the full path is '/users/' to reach the route.Final Answer:
/users/ -> Option AQuick Check:
Mount path + router route = full URL [OK]
- Ignoring mount path and using '/' alone
- Confusing '/user/' with '/users/'
- Assuming '/users/list' without route defined
const express = require('express');
const app = express();
const router = express.Router();
router.get('/info', (req, res) => res.send('Info page'));
app.use(router, '/info');Solution
Step 1: Check
The correct order isapp.useargument orderapp.use(path, router), but here it is reversed.Step 2: Identify the error cause
Passing router first and path second causes Express to treat router as middleware without a path, leading to unexpected behavior.Final Answer:
The arguments to app.use are in the wrong order -> Option DQuick Check:
app.use(path, router) order matters [OK]
- Swapping router and path in app.use
- Thinking router paths can't have '/info'
- Ignoring missing app.listen (not related here)
adminRouter and userRouter, so that /admin/dashboard and /users/profile work correctly. Which code correctly mounts both routers?Solution
Step 1: Understand mounting routers at base paths
Mount routers at base paths like '/admin' and '/users' so their internal routes append correctly.Step 2: Check each option for correct syntax and paths
app.use('/admin', adminRouter);\napp.use('/users', userRouter); usesapp.use('/admin', adminRouter);andapp.use('/users', userRouter);, which is correct. app.use(adminRouter, '/admin');\napp.use(userRouter, '/users'); swaps arguments, C mounts at full paths (not base), D uses app.get which is incorrect for mounting routers.Final Answer:
app.use('/admin', adminRouter); app.use('/users', userRouter); -> Option BQuick Check:
Mount routers at base paths with app.use(path, router) [OK]
- Swapping router and path in app.use
- Mounting routers at full nested paths
- Using app.get instead of app.use for routers
Ready to prove it?
Pass the test and this pattern moves unknown
