Express.js | app.route() Function
Last Updated :
08 Jan, 2025
The app.route() function returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names (and thus typo errors).
Syntax:
app.route( path )
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:

Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.route('/user')
.get((req, res, next) => {
res.send('GET request called');
})
.post((req, res, next) => {
res.send('POST request called');
})
.all((req, res, next) => {
res.send('Other requests called');
})
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, if we make a POST request to /user we get a POST request called, similarly if we make a GET request to /user we get a GET request called, and so on.
So this is how you can use the express app.route() function which returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware.
Similar Reads
Express.js router.all() Function 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
2 min read
Express.js | app.path() Function The app.path() function returns the canonical path of the app as a string. When mounting advanced apps, the behavior of this approach can become extremely complicated. In most cases, it is better to use req.baseUrl to obtain the app's canonical path. Installation of the express module: You can visit
2 min read
Express app.use() Function The app.use() function in Express.js adds middleware to the application's request-processing pipeline. It applies the specified middleware to all incoming requests or to specific routes, allowing you to modify request/response objects, perform operations, or handle errors throughout the application.
3 min read
Express.js | app.param() Function The app.param() function is used to add the callback triggers to route parameters. It is commonly used to check for the existence of the data requested related to the route parameter. Syntax: app.param([name], callback) Parameters: name: It is the name of the parameter or an array of them.callback:
2 min read
Express app.listen() Function In Express.js, the app.listen() function is used to start a server and make it listen for incoming requests on a specified port and host. This method is essential for running an Express application as it enables the server to handle HTTP requests from clients (like browsers).Syntax: app.listen([port
3 min read