Express.js app.param() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

app.param(name, callback) adds a trigger for a named route parameter such as :id or :user. When a matching route 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 paths from app.get() and values in req.params.

Syntax

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

Express
app.param(name, callback)
app.param([name1, name2], callback)
CallWhat it does
app.param(name, callback)Primary signature.
app.param([name1, name2], callback)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();

app.param(name, callback)
app.param([name1, name2], callback)

Parameters

ParameterDescription
nameRequired. Parameter name without the colon — 'id' for :id. An array registers the callback for each name.
callbackRequired. (req, res, next, value, name). Call next() to continue, next(err) on failure, or next('route') to skip this route.

Return Value

app.param() returns the same application object (app), which is usually ignored. When you pass an array of names, the same callback is registered for each name in order.

Express
app.param('id', loadById);
// => app

app.param(['userId', 'postId'], loadById);

Express.js Examples

Example 1 — Validate :id

Reject non-numeric ids before the route handler runs.

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

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

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

app.listen(3000);

How It Works

  1. Register for id. Any route with :id on this app will trigger the callback.
  2. Check the value. Digits-only pass; anything else gets a 400.
  3. Normalize. Store a Number back on req.params.id for the handler.
  4. Continue. next() runs the GET /users/:id handler.

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

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

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

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

app.listen(3000);

How It Works

  1. One param hook. app.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 users = {
  ada: { name: 'Ada', role: 'admin' },
  lin: { name: 'Linus', role: 'dev' }
};

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

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

app.listen(3000);

How It Works

  1. Named param user. Matches routes 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.

Runtime Support

app.param() is an Express.js application 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

app.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
app.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 in a matching route, before the route handlers.
callback(req, res, next, value, name) — the parameter value and its name come after next.
No. Even if several routes match the same param name, the param callback runs only once per request-response cycle.
No. They are local to the app or router where you define them. Mounted apps/routers need their own app.param / router.param.
No. It is server-side Express middleware wired to route parameters on Node.js.

Next: app.path()

Inspect the canonical path of nested Express apps.

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