Open In App
Related Articles

Express.js | app.use() Function

Improve Article
Save Article
Like Article

The app.use() function is used to mount the specified middleware function(s) at the path which is being specified. It is mostly used to set up middleware for your application. 


app.use(path, callback)


  • path: It is the path for which the middleware function is being called. It can be a string representing a path or path pattern or a regular expression pattern to match the paths.
  • callback: It is a middleware function or a series/array of middleware functions.

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 


const express = require('express');
const app = express();
const PORT = 3000;
// This middleware will not allow the
// request to go beyond it
app.use(function (req, res, next) {
    console.log("Middleware called")
// Requests will never reach this route
app.get('/user', function (req, res) {
    console.log("/user request called");
    res.send('Welcome to GeeksforGeeks');
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


Console Output:

Server listening on PORT 3000

Browser Output:

Now open your browser and go to http://localhost:3000/user and you can see the following output on the console as shown below:

Server listening on PORT 3000
Middleware called
/user request called

And on the browser, you will see the Welcome to GeeksforGeeks.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials