Open In App

Express.js | router.use() Function

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

The router.use() function uses the specified middleware function or functions. It basically mounts middleware for the routes which are being served by the specific router. 

Syntax:

router.use( path, function )

Parameters:

  • Path: It is the path to this middleware, if we can have /user, now this middleware is called for all API’s having /user of this router.
  • function: This function is passed as a callback, it is called when the specified path is called in this router.

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 router = express.Router();
const PORT = 3000;
 
// All requests to this router will
// first hit this middleware
router.use(function (req, res, next) {
    console.log("Middleware Called");
    next();
})
 
// Always invoked
router.use(function (req, res, next) {
    res.send("Greetings from GeeksforGeeks");
})
 
app.use('/user', router);
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

The project structure will look like this: 

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:

Server listening on PORT 3000

Now open your browser and go to http://localhost:3000/user, you can see the following output on your screen:

Server listening on PORT 3000
Middleware Called

And you will see the following output on your browser:

Greetings from GeeksforGeeks

Previous Article
Next Article

Similar Reads

Express express.Router() Function
The express.Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.  Syntax: express.Router( [options] )Optional Parameters: case-sensitive: This enables case sensitivity.mergeParams: It preserves the req.params values from the parent router.stric
2 min read
Purpose of the express.Router middleware in Express
express.Router() in ExpressJS is like creating a mini-app within your main app. It helps organize routes and related functionalities, making it easier to manage different parts of your web application separately. express.Router() acts as a path organizer in ExpressJS, helping to structure and manage routes efficiently within a web application. Purp
1 min read
What is the use of router in Express.js ?
Express.js is a powerful framework for node.js. One of the main advantages of this framework is defining different routes or middleware to handle the client's different incoming requests. In this article, we will discuss, how to use the router in the express.js server. The express.Router() function is used to create a new router object. This functi
2 min read
How to Create Custom Router with the help of React Router ?
To create a custom Router using React Router, you can define a new component that utilizes the Router's 'BrowserRouter' along with 'Route' components to handle different paths and render corresponding components. Use 'Link' components for navigation within your application, providing a seamless routing experience in your React application. What is
3 min read
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 of the specified URL and the callback is the fu
2 min read
Express.js router.route() Function
The router.route() function returns an instance of a single route that you can then use to handle HTTP verbs with optional middleware. You can also use the router.route() function to avoid duplicate route naming as well as typing errors. Syntax: router.route( path ) Parameter: The path parameter holds the path of the specified URL. Return Value: It
2 min read
Express.js router.METHOD() Function
The router.METHOD() method provides the routing functionality in Express, where METHOD is one of the HTTP methods, such as GET, PUT, POST, and so on, in lowercase. Syntax: router.METHOD(path, [callback, ...] callback) Parameter: The path parameter specifies the path on the URL and callback is the function that is executed when this path is called.
2 min read
Express.js router.param() function
The parameters of router.param() are a name and function. Where the name is the actual name of the parameter and the function is the callback function. Basically, the router.param() function triggers the callback function whenever the user routes to the parameter. This callback function will be called only a single time in the request-response cycl
2 min read
Build Your First Router in Node.js with Express
Express.js is a powerful framework for node.js. One of the main advantages of this framework is defining different routes or middleware to handle the client's different incoming requests. In this article, we will discuss, how to use the router in the express.js server. The express.Router() function is used to create a new router object. This functi
2 min read
Express.js app.router Property
The Express.js app.router property was introduced in Express 4. It helps us to create modular, mountable route handlers. It provides us with many features such as it extends this routing to handle validation, handle 404 or other errors, etc. It helps us to organize our file structure for our server-side programming. What is the need to use express.
3 min read