0
0
ExpressHow-ToBeginner · 4 min read

How to Create Modular Routes in Express for Cleaner Code

To create modular routes in Express, define routes in separate files using express.Router(), then export and import them into your main app file. Use app.use() to mount these routers on specific path prefixes for clean and organized routing.
📐

Syntax

Modular routes in Express use the express.Router() class to create route handlers in separate files. You export the router and import it in your main app file. Then, use app.use(path, router) to mount the routes.

  • const router = express.Router(); - creates a new router instance.
  • router.get('/path', handler) - defines a route on the router.
  • module.exports = router; - exports the router for use elsewhere.
  • app.use('/prefix', router); - mounts the router on a path prefix.
javascript
const express = require('express');
const router = express.Router();

// Define routes on the router
router.get('/example', (req, res) => {
  res.send('Example route');
});

module.exports = router;

// In main app file
const express = require('express');
const app = express();
const exampleRouter = require('./exampleRouter');

app.use('/example', exampleRouter);

app.listen(3000);
💻

Example

This example shows how to create a modular route for user-related paths. The users.js file defines routes for listing users and getting a user by ID. The main app file imports and mounts this router under /users.

javascript
// users.js
const express = require('express');
const router = express.Router();

// Route to get all users
router.get('/', (req, res) => {
  res.send('List of users');
});

// Route to get a user by ID
router.get('/:id', (req, res) => {
  res.send(`User with ID: ${req.params.id}`);
});

module.exports = router;

// app.js
const express = require('express');
const app = express();
const usersRouter = require('./users');

app.use('/users', usersRouter);

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
Output
Server running on http://localhost:3000
⚠️

Common Pitfalls

Common mistakes when creating modular routes include:

  • Not exporting the router from the route file, so it can't be imported.
  • Forgetting to use app.use() to mount the router in the main app.
  • Using absolute paths inside routers without considering the mount path, causing unexpected route URLs.
  • Defining routes directly on app instead of the router when modularizing.
javascript
// Wrong: Not exporting router
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => res.send('Hello'));
// Missing: module.exports = router;

// Right: Export router
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => res.send('Hello'));
module.exports = router;

// Wrong: Forgetting to mount router
const express = require('express');
const app = express();
const router = require('./router');
// Missing: app.use('/path', router);

// Right: Mount router
const express = require('express');
const app = express();
const router = require('./router');
app.use('/path', router);
📊

Quick Reference

Tips for modular routes in Express:

  • Use express.Router() to create route modules.
  • Export routers with module.exports.
  • Import and mount routers in the main app with app.use().
  • Keep route files focused on related routes for clarity.
  • Use path prefixes in app.use() to organize URLs.

Key Takeaways

Use express.Router() to create modular route handlers in separate files.
Always export your router and import it in the main app file.
Mount routers on path prefixes with app.use() for organized URLs.
Avoid defining routes directly on app when modularizing.
Check that routers are properly exported and mounted to avoid routing errors.