Express.js express.Router() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
express()

Definition

In Express.js, express.Router() creates a new router — a mini-application that only does middleware and routing. You add get, post, use, and other handlers on it, then mount the whole stack with app.use('/prefix', router).

Use it to split features into files (users API, admin API, calendar) without putting every route on the top-level app. A router also works as middleware, so you can nest routers with router.use().

Syntax

Call express.Router in your Express.js app. Start with this line.

Express
express.Router([options])
CallWhat it does
express.Router([options])Primary signature.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

express.Router([options])

Parameters

ParameterDescription
optionsOptional. Router behavior settings. Omit for defaults.
options.caseSensitiveOptional. Default false: /Foo and /foo match the same. true treats them as different.
options.mergeParamsOptional. Default false. true keeps parent req.params in the child. On name conflicts, the child value wins.
options.strictOptional. Default false: /foo and /foo/ are the same. true treats trailing slashes as different.

Return Value

express.Router([options]) returns a new router instance. Pass an optional options object to control case sensitivity, trailing slashes, and parent param merging.

Express
const router = express.Router({ mergeParams: true });
router.get('/:id', show);
app.use('/users', router);

Express.js Examples

Example 1 — Create and Mount a Router

Define routes on a router, then attach it under /calendar.

Express
const express = require('express');
const app = express();
const router = express.Router();

router.get('/events', (req, res) => {
  res.send('events');
});

app.use('/calendar', router);

app.listen(3000);

How It Works

  1. Create the stack. express.Router() returns an empty router.
  2. Add a relative route. router.get('/events') is relative to wherever you mount the router.
  3. Mount with a prefix. app.use('/calendar', router) means the full path is /calendar/events.
  4. Only prefixed requests enter. A bare /events never reaches this router.

Example 2 — Middleware on the Router

Run shared logic for every request that enters this router, then handle a route.

Express
const express = require('express');
const app = express();
const router = express.Router();

router.use((req, res, next) => {
  console.log('calendar router:', req.method, req.url);
  next();
});

router.get('/events', (req, res) => {
  res.json({ ok: true });
});

app.use('/calendar', router);

app.listen(3000);

How It Works

  1. Scope middleware to the module. router.use runs only for requests that already matched the mount path.
  2. Call next(). Without it, later routes on this router never run.
  3. Relative URL inside the router. After the mount strips /calendar, the router often sees /events.
  4. Then the route answers. router.get('/events') sends the JSON response.

Example 3 — Parent Params with mergeParams

Nest a child router under a param path and keep the parent id on req.params.

Express
const express = require('express');
const app = express();

const parent = express.Router();
const child = express.Router({ mergeParams: true });

parent.use('/user/:userId', child);

child.get('/comments', (req, res) => {
  res.send('Comments for user ' + req.params.userId);
});

app.use(parent);

app.listen(3000);

How It Works

  1. Parent owns the param. /user/:userId captures the id on the parent mount.
  2. Child opts in. express.Router({ mergeParams: true }) copies parent params into the child.
  3. Child route stays short. child.get('/comments') only cares about the suffix.
  4. Read the merged param. req.params.userId works inside the child handler. If names clash, the child’s value wins.

Runtime Support

express.Router() is an Express.js factory on the express module. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it.

✓ Server · Express 4 & 5

express.Router()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
express.Router() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It creates a new router — a mini-app that holds middleware and routes. You mount it on the main app (or another router) with app.use() or router.use().
The app is the full Express application (settings, listen, engines). A Router only does middleware and routing, so you can split APIs into modules.
With mergeParams: true, a child router keeps parent path params on req.params. If both define the same name, the child’s value wins.
No. Defaults are fine for most apps: case-insensitive paths, trailing slashes treated the same, and no parent param merge.
No. Routers run on your Node.js server. Browsers only send HTTP requests to the mounted paths.

Next: express.static()

Serve CSS, JS, and other files from a folder with built-in static middleware.

express.static() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful