The router.route() function returns a route instance for a specified path. It can then be used to define handlers for multiple HTTP methods, such as GET, POST, PUT, and DELETE, on the same path.Â

Syntax:
router.route( path )Parameter: The path for which the route is defined.
Return Value: Returns a Route instance that can be used to define handlers for different HTTP methods.
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 expressAfter installing the express module, you can check your express version in the command prompt using the command.
npm version expressAfter 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.jsProject Structure:

Example 1: Filename: index.jsÂ
const express = require('express');
const app = express();
const router = express.Router();
const PORT = 3000;
// Single routing
router.route('/user')
.get(function (req, res, next) {
console.log("GET request 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 expressRun the index.js file using the below command:
node index.jsOutput:
Console Output:
Server listening on PORT 3000Browser Output:
Now make a GET request to http://localhost:3000/user, you can see the following output on your screen:

Example 2: Filename: index.jsÂ
const express = require('express');
const app = express();
const router = express.Router();
const PORT = 3000;
// Multiple routing
router.route('/user')
.get(function (req, res, next) {
console.log("GET request called");
res.end();
})
.post(function (req, res, next) {
console.log("POST request called");
res.end();
})
.put(function (req, res, next) {
console.log("PUT request 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.jsOutput: Now make GET, POST, and PUT requests to http://localhost:3000/user, you can see the following output on your screen:

Working of router.route()
- An Express Router instance is created using express.Router().
- A route is created for a specific path using router.route().
- HTTP method handlers such as .get(), .post(), and .put() are chained to the route.
- Express matches the incoming request with the corresponding HTTP method and path.
- The appropriate callback function is executed when a match is found.
Use Cases of router.route()
- Defining multiple HTTP methods for the same path.
- Keeping related route handlers together.
- Avoiding repetition of the same route path.
- Organizing routes in modular Express applications.
- Building RESTful APIs.
Reference:Â https://expressjs.com/en/4x/api.html#router.route