Open In App

Express.js router.all() Function

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The router.all() function is just like the router.METHOD() methods, except that it matches all HTTP methods (verbs). It is very helpful for mapping global logic for arbitrary matches or specific path prefixes. 

Syntax:

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

Parameter: The path parameter is the path of the specified URL and the callback is the function passed as a parameter. 

Return Value: It returns responses. 

Installation of the express module:

You can visit the link to Install 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:

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:

Make sure you have installed the express module using the following command:

npm install express

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

User Page Called    

Every type of request made to http://localhost:3000/user will print the same output.

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.

User Page Called 
Student Page Called 
Teacher Page Called    

Reference: https://expressjs.com/en/4x/api.html#router.all



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads