Express.js router.all() Function - GeeksforGeeks

Express.js router.all() Function

Last Updated : 25 Sep, 2026

The router.all() function is used to define a route that matches requests made with all HTTP methods. It works similarly to router.METHOD(), but instead of matching only a specific HTTP method such as GET or POST, it matches all HTTP methods supported by Express.

Syntax:

router.all(path, [callback, ...] callback)

Parameter:

  • path: The path for which the route callbacks are invoked.
  • callback: A middleware or handler function that is executed when a request matches the specified path. Multiple callback functions can also be provided.

Return Value: Returns the Router instance.

Installation of the Express module

You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

Screenshot-2026-08-03-153443

Example 1: Filename: index.js 

javascript
const express = require('express');
const app = express();
const router = express.Router();
const PORT = 3000;
// Setting single route
router.all('/user', function (req, res) {
    console.log("User Page Called");
    res.end();
});
app.use(router);

app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Console Output:

Server listening on PORT 3000

Browser Output:

Now make any request to http://localhost:3000/user like POST, PUT, DELETE, or any other type of request, and it will be shown the following output

Screenshot-2026-08-22-121431

Example 2: Filename: index.js 

javascript
const express = require('express');
const app = express();
const router = express.Router();
const PORT = 3000;
// Setting multiple routes
router.all('/user', function (req, res) {
    console.log("User Page Called");
    res.end();
});
router.all('/student', function (req, res) {
    console.log("Student Page Called");
    res.end();
});
router.all('/teacher', function (req, res) {
    console.log("Teacher Page Called");
    res.end();
});
app.use(router);
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Now make a GET request to http://localhost:3000/user, http://localhost:3000/student, and http://localhost:3000/teacher it will show the following output.

Screenshot-2026-08-22-122018

Working of router.all()

  • An Express Router instance is created using express.Router().
  • A route is defined using router.all().
  • Express checks the incoming request against the specified path.
  • If the path matches, the callback functions are executed regardless of the HTTP method.
  • The callback can process the request, send a response, or call next() to continue to the next middleware or route.
  • The router is mounted on the Express application using app.use().

Use Cases of router.all()

  • Handling all HTTP methods for a specific route.
  • Applying authentication or authorization to a group of routes.
  • Implementing request logging for a path.
  • Applying common processing before route handlers.
  • Handling global logic for specific path prefixes.
  • Building modular Express applications and RESTful APIs.
Comment