Express.js app.router Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

In Express.js, express.Router() creates a mini-application for middleware and routing only. You add get/post/… routes on the router, then mount it with app.use('/prefix', router).

Official docs: a router behaves like middleware, so you can also nest routers. Every Express app also has a built-in router; this page focuses on creating modular route files with express.Router().

Syntax

Read app.router in your Express.js route handler. Start with this line.

Express
const router = express.Router([options])
router.get(path, ...callbacks)
router.post(path, ...callbacks)
router.use([path], ...callbacks)
app.use('/prefix', router)
CallWhat it does
const router = express.Router([options])Primary signature.
router.get(path, ...callbacks)Related call form.
router.post(path, ...callbacks)Related call form.
router.use([path], ...callbacks)Related call form.

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();

const router = express.Router([options])
router.get(path, ...callbacks)
router.post(path, ...callbacks)
router.use([path], ...callbacks)
app.use('/prefix', router)

Parameters

ParameterDescription
caseSensitiveRequired. When true, /Foo and /foo are different.
mergeParamsRequired. Keep parent req.params. Child values win on name conflicts.
strictRequired. When true, /foo and /foo/ are different.

Return Value

Creating a router (via express.Router() / the app router APIs shown on this page) returns a Router instance. Its method APIs match the app (get, post, all, route, param, …).

Express
const router = express.Router();
router.get('/events', list);
app.use('/api', router);

Express.js Examples

Example 1 — Mount a Router

Official pattern: define routes on the router, then app.use('/calendar', router).

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

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

// only /calendar/* hits this router
app.use('/calendar', router);

app.listen(3000);

How It Works

  1. Create. express.Router() makes an empty mini-app.
  2. Local path. router.get('/events') is relative to the mount.
  3. Mount. app.use('/calendar', router) prefixes every route.
  4. Full URL. Clients call /calendar/events.

Example 2 — Router Middleware

Middleware on the router runs for requests that enter that mount.

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

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

users.get('/', (req, res) => {
  res.json([{ id: 1, name: 'Ada' }]);
});

users.get('/:id', (req, res) => {
  res.json({ id: req.params.id });
});

app.use('/users', users);
app.listen(3000);

How It Works

  1. Scoped middleware. users.use runs only for /users….
  2. List route. GET / on the router is GET /users on the app.
  3. Param route. /:id becomes /users/:id.
  4. next(). Middleware must call next() so handlers run.

Example 3 — mergeParams

Keep a parent :orgId inside a nested router.

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

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

teams.get('/', (req, res) => {
  res.json({
    orgId: req.params.orgId,
    teamId: req.params.teamId || null
  });
});

teams.get('/:teamId', (req, res) => {
  res.json({
    orgId: req.params.orgId,
    teamId: req.params.teamId
  });
});

app.use('/orgs/:orgId/teams', teams);
app.listen(3000);

How It Works

  1. Parent param. Mount path includes :orgId.
  2. mergeParams: true. Child keeps req.params.orgId.
  3. Child param. :teamId is added on the nested route.
  4. Conflicts. If both defined the same name, the child’s value wins.

Runtime Support

express.Router() is an Express.js API. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only hit the mounted URLs.

✓ 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.

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
Router Express on Node.js

Bottom line: Create a router, mount with app.use('/prefix', router), then request the full path.

Frequently Asked Questions

A Router is a mini-application that only does middleware and routing. You add routes to it, then mount it with app.use('/prefix', router).
const router = express.Router(); define router.get/post/…; then app.use('/api', router). Requests to /api/… are handled by that router.
When true, the child router keeps parent req.params. If names conflict, the child’s value wins. Default is false.
No. app.route(path) chains verbs on one path. express.Router() is a whole module of routes you can mount under a prefix.
No. It is a server-side Express API on Node.js.

Next: app.set()

Configure Express application settings like views and view engine.

app.set() 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