How to Use express.Router for Modular Routing in Express
Use
express.Router() to create a modular route handler in Express. Define routes on the router instance, then use app.use() to mount it in your main app for cleaner and organized routing.Syntax
The express.Router() function creates a new router object. You define routes on this router just like on the main app. Then, mount the router on a path using app.use().
const router = express.Router();- creates a new router.router.get(path, handler)- defines a GET route on the router.app.use(basePath, router)- mounts the router on the app atbasePath.
javascript
const express = require('express'); const app = express(); const router = express.Router(); router.get('/example', (req, res) => { res.send('Hello from router!'); }); app.use('/api', router); app.listen(3000);
Example
This example shows how to create a router for user-related routes and mount it under /users. It keeps the main app file clean and organizes routes by feature.
javascript
const express = require('express'); const app = express(); // Create a router for user routes const userRouter = express.Router(); // Define routes on the userRouter userRouter.get('/', (req, res) => { res.send('User list'); }); userRouter.get('/:id', (req, res) => { res.send(`User details for ID: ${req.params.id}`); }); // Mount the userRouter on /users path app.use('/users', userRouter); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Output
Server running on http://localhost:3000
Common Pitfalls
Common mistakes when using express.Router() include:
- Forgetting to mount the router with
app.use(), so routes never get called. - Not using the correct base path when mounting, causing routes to be unreachable.
- Defining routes on
appinstead of the router or mixing both confusingly.
Always define routes on the router instance and mount it properly.
javascript
/* Wrong: defining routes on app but mounting router */ const express = require('express'); const app = express(); const router = express.Router(); app.get('/test', (req, res) => { res.send('This works'); }); app.use('/api', router); // router has no routes /* Right: define routes on router and mount it */ router.get('/test', (req, res) => { res.send('This works'); }); app.use('/api', router);
Quick Reference
express.Router() Quick Reference:
const router = express.Router();- create a new router.router.METHOD(path, handler)- define routes (GET, POST, etc.).app.use(basePath, router);- mount router on app.- Use routers to organize routes by feature or module.
- Helps keep main app file clean and maintainable.
Key Takeaways
Use express.Router() to create modular route handlers for better code organization.
Define routes on the router instance, not directly on the app when using routers.
Mount the router on a base path with app.use() to activate its routes.
Routers help keep your main app file clean and routes grouped logically.
Always check your base path and route paths to avoid unreachable routes.