Express.js router.param() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Router

Definition

router.param(name, callback) adds a trigger for a named route parameter such as :id or :user on an express.Router(). When a matching route on that router runs, Express calls your callback before the route handlers — ideal for validation and loading data into req.

The callback signature is (req, res, next, value, name). Pair it with router.METHOD() paths and values in req.params. The app-level twin is app.param().

Syntax

Call router.param in your Express.js app. Start with this line.

Express
router.param(name, callback)
CallWhat it does
router.param(name, callback)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();

router.param(name, callback)

Parameters

ParameterDescription
nameRequired. Parameter name without the colon — 'id' for :id. Arrays are not allowed on routers.
callbackRequired. (req, res, next, value, name). Call next() to continue, next(err) on failure, or next('route') to skip this route.

Return Value

router.param(name, callback) returns the same router (usually ignored). name is required and must be a string.

Express
router.param('id', loadUser);
// => router

router.get('/:id', show);

Express.js Examples

Example 1 — Validate :id on a Mounted Router

Reject non-numeric ids before the route handler runs. Public URL is /users/:id.

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

users.param('id', (req, res, next, value) => {
  if (!/^\d+$/.test(value)) {
    return res.status(400).send('id must be a number');
  }
  req.params.id = Number(value);
  next();
});

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

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

How It Works

  1. Register on the router. users.param('id', …) only watches routes defined on users.
  2. Mount strips the prefix. Inside the router the path is /:id; the public URL is /users/:id.
  3. Validate, then normalize. Digits pass and become a Number on req.params.id.
  4. Continue or stop. next() reaches users.get('/:id'); a bad id never does.

Example 2 — Called Only Once

Official behavior: two matching routes still run the param callback a single time.

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

router.param('id', (req, res, next, id) => {
  console.log('CALLED ONLY ONCE');
  next();
});

router.get('/user/:id', (req, res, next) => {
  console.log('although this matches');
  next();
});

router.get('/user/:id', (req, res) => {
  console.log('and this matches too');
  res.end();
});

app.use(router);
app.listen(3000);

How It Works

  1. One param hook. router.param('id', …) is shared by both routes.
  2. First route. Logs and calls next() so the stack continues.
  3. Second route. Also matches /user/:id and ends the response.
  4. Once per cycle. The param log appears only once, not twice.

Example 3 — Preload a User

Docs-style loading: attach req.user when :user is present (in-memory map).

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

const users = {
  ada: { name: 'Ada', role: 'admin' },
  lin: { name: 'Linus', role: 'dev' }
};

members.param('user', (req, res, next, id) => {
  const found = users[id];
  if (!found) {
    return res.status(404).send('User not found');
  }
  req.user = found;
  next();
});

members.get('/profile/:user', (req, res) => {
  res.json(req.user);
});

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

How It Works

  1. Named param user. Matches routes on this router with :user.
  2. Lookup. Find the record (here a plain object; in apps often a DB call).
  3. Attach. Set req.user so the route stays thin.
  4. 404 early. Missing users never reach the JSON handler. The mount makes the public path /members/profile/:user.

Runtime Support

router.param() is an Express.js router method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send the URL that contains the parameter.

✓ Server · Express 4 & 5

router.param()

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.param() Express on Node.js

Bottom line: Run examples with Node.js and the express package, then request paths like /users/42.

Frequently Asked Questions

It registers a callback that runs when a named route parameter (like :id) appears on that router’s routes, before the route handlers.
Behavior is the same for a single name, but router.param() is scoped to one router and does not accept an array of names. app.param() can register the same callback for several names at once.
No. Even if several routes on the router match the same param name, the param callback runs only once per request-response cycle.
No. Param callbacks are local. They are not triggered for parameters defined only on a parent router, even when mergeParams makes those values visible on req.params.
No. It is server-side Express middleware wired to route parameters on Node.js.

Next: router.route()

Chain GET, POST, and PUT on one path after your param hooks.

router.route() 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