Express.js router.all() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Router

Definition

In Express.js, router.all() registers handlers on an express.Router() that run for every HTTP method. After you mount the router with app.use('/users', router), router.all('/profile', …) matches GET, POST, PUT, and DELETE to /users/profile.

Use it for shared module logic — logging, auth, or loading a user — then call next() so a method route such as router.get() can finish the response. The path is relative to the mount. It is not a browser API; it runs inside your Express server.

Syntax

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

Express
router.all(path, callback)
router.all(path, ...callbacks)
CallWhat it does
router.all(path, callback)Primary signature.
router.all(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();

router.all(path, callback)
router.all(path, ...callbacks)

Parameters

ParameterDescription
pathRequired. Path relative to the router’s mount. Strings match the whole remaining path. Arrays match any listed path.
callbackRequired. One or more handlers: (req, res, next) => { … }. Can call next('route') to skip remaining callbacks for this route.

Return Value

router.all() returns the same router instance, so chaining is possible. You can pass one callback or several; extra callbacks behave like middleware.

Express
router.all('/profile', gate).get('/profile', show);
// or typical style:
router.all('/profile', gate);
router.get('/profile', show);

Express.js Examples

Example 1 — Log Every Method on a Mounted Path

Print the method and public URL for any request to /users/profile, then continue.

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

users.all('/profile', (req, res, next) => {
  console.log(req.method, req.baseUrl + req.path);
  next();
});

users.get('/profile', (req, res) => {
  res.send('Profile GET');
});

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

How It Works

  1. Mount the router. app.use('/users', users) strips /users before the router sees the request. Inside the router the path is /profile.
  2. Match the method-free path. users.all('/profile') runs for GET, POST, and the other verbs. req.baseUrl is /users; req.path is /profile.
  3. Log, then continue. next() hands control to the next matching handler.
  4. GET gets a reply. users.get('/profile') sends Profile GET. POST still logs, then Express returns 404 because no POST route exists.

Example 2 — Auth Gate for the Whole Router

Require a header before any method on this router can continue. Express 5 uses a named splat so the gate includes the mount root.

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

api.all('/{*splat}', (req, res, next) => {
  if (req.get('x-api-key') !== 'demo-key') {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  next();
});

api.get('/profile', (req, res) => {
  res.json({ name: 'Ada' });
});

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

How It Works

  1. Cover this router. /{*splat} matches the router root (/api) and deeper paths such as /api/profile.
  2. Check once for all methods. GET and POST both hit the same gate before any api.get or api.post.
  3. Deny with a response. A missing key sends 401 and does not call next(), so later routes never run.
  4. Allow with next(). A valid key continues to api.get('/profile'), which sends the JSON.

Example 3 — Several Callbacks on One router.all

Chain shared steps on one mounted path: require a user header, attach the user, then answer with GET.

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

function requireAuth(req, res, next) {
  if (!req.get('x-user')) {
    return res.status(401).send('Login required');
  }
  next();
}

function loadUser(req, res, next) {
  req.user = { id: 1, name: req.get('x-user') };
  next();
}

members.all('/account', requireAuth, loadUser);

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

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

How It Works

  1. Pass a list of handlers. members.all('/account', requireAuth, loadUser) runs them in order for every method. The public URL is /members/account.
  2. Gate first. requireAuth stops with 401 when the header is missing.
  3. Enrich the request. loadUser sets req.user, then calls next().
  4. Answer in the method route. members.get('/account') sends the loaded user as JSON.

Runtime Support

router.all() is an Express.js router method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it — they only send HTTP requests to your app.

✓ Server · Express 4 & 5

router.all()

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
router.all() 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 registers one or more handlers on an Express Router for a path that run for every HTTP method — GET, POST, PUT, PATCH, DELETE, and the others.
The verb behavior is the same. router.all() lives on a router, so the path is relative to where you mount that router with app.use(). It does not see routes registered on the main app or on a different router.
router.all() is a route for one path and every HTTP method. router.use() mounts middleware or another router, often as a prefix that also matches paths underneath it.
Yes, if a later handler should run. Sending a response (res.send, res.json, and so on) without next() stops the chain — correct for a 401. next('route') skips the rest of this route and lets Express try the next matching route.
No. Express runs on a Node.js (or compatible) server. Browsers only send HTTP requests; they do not execute router.all().

Next: router.METHOD()

Handle one HTTP verb at a time inside the same router module.

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