Skip to content

Modular Route Routers

If we keep piling every single route into our main app.js file, we’re going to create a massive traffic jam. Navigating a 500-line file to find one route is like trying to drive through downtown during rush hour—inefficient and infuriating.

We use Express Router to build modular, “local” road networks.

A sleek, multi-level highway system where specific types of futuristic vehicles are segregated into their own dedicated, glowing lanes.

Fig 1: Segregated Traffic. Clean lines, high speed.

Professor Solo’s Directive (Speed vs Sanity): When we talk about “speed” here, we are talking about development speed. Splitting your code into multiple files does not make the Node server run faster. In fact, it’s technically a microsecond slower due to the extra file lookups.

But we don’t care. We do this because working on a 10,000 line app.js file is a nightmare. Modular code enables teams to move fast without crashing into each other.


  1. Create a Router File (e.g., routes/thingRouter.js).
  2. Define Routes on that router, not the main app.
  3. Export the router.
const express = require('express');
const router = express.Router(); // A mini-app instance
// We define our routes here
// Note: We are using the FULL path for now.
router.get('/things', (req, res) => {
res.send('List of Things');
});
router.get('/things/:id', (req, res) => {
res.send('Details of Thing');
});
module.exports = router;

Now we have a self-contained module. But it’s not connected to the highway yet.


Express Router (No Base Path) - Demo Repo

Express Router API

⏭ On-Ramps

The local road is built. Now we need to connect it to the main highway using a Base Path.