0
0
ExpressHow-ToBeginner · 3 min read

How to Organize Routes in Express for Clean Code

In Express, organize routes by using express.Router() to create modular route handlers and placing them in separate files. Then, import and use these routers in your main app with app.use() for clean and maintainable code.
📐

Syntax

Use express.Router() to create a new router object. Define routes on this router instead of the main app. Export the router from a module and import it in your main app file. Use app.use(path, router) to mount the router on a path prefix.

  • express.Router(): creates a modular route handler.
  • router.get/post/put/delete(): define routes on the router.
  • module.exports = router: export the router.
  • app.use('/prefix', router): mount router under a URL prefix.
javascript
const express = require('express');
const router = express.Router();

// Define routes on router
router.get('/', (req, res) => {
  res.send('Hello from router root');
});

module.exports = router;

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

app.use('/mypath', myRouter);

app.listen(3000);
💻

Example

This example shows how to split routes into a separate file using express.Router() and mount it in the main app. It demonstrates modular route organization for clarity and scalability.

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

// Route for GET /users/
router.get('/', (req, res) => {
  res.send('User list');
});

// Route for GET /users/:id
router.get('/:id', (req, res) => {
  res.send(`User details for ID: ${req.params.id}`);
});

module.exports = router;

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

// Mount users router at /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

  • Defining all routes directly in app.js makes the file large and hard to maintain.
  • Forgetting to use module.exports to export routers causes import errors.
  • Not mounting routers with app.use() means routes won't be reachable.
  • Using the wrong path prefix when mounting routers can cause unexpected URLs.
javascript
// Wrong: Defining routes but 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
module.exports = router;

// Wrong: Forgetting to mount router
const usersRouter = require('./routes/users');
// Missing: app.use('/users', usersRouter);

// Right: Mount router
app.use('/users', usersRouter);
📊

Quick Reference

Tips for organizing Express routes:

  • Use express.Router() to create route modules.
  • Group related routes in separate files (e.g., routes/users.js).
  • Export routers with module.exports.
  • Mount routers in app.js with app.use('/prefix', router).
  • Keep app.js focused on app setup and middleware.

Key Takeaways

Use express.Router() to create modular route handlers for better organization.
Place related routes in separate files and export the router from each.
Mount routers in your main app with app.use() and a path prefix.
Avoid putting all routes in one file to keep code clean and maintainable.
Always export routers and correctly mount them to ensure routes work.