Express.js | app.use() Function - GeeksforGeeks

18 Jun, 2020
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.
Syntax:
app.use(path, callback)
Parameters:
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 regular expression pattern to match the paths.callback: It is a middleware function or a series/array of middleware functions.
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 regular expression pattern to match the paths.
callback: It is a middleware function or a series/array of middleware functions.
Installation of express module:
You can visit the link to Install express module. You can install this package by using this command.npm install expressAfter installing express module, you can check your express version in 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.js
You can visit the link to Install express module. You can install this package by using this command.npm install express
npm install express
After installing express module, you can check your express version in command prompt using the command.npm version express
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
node index.js
Filename: index.js
var express = require('express');var app = express();var PORT = 3000;  // This middleware will not allow the// request to go beyond itapp.use(function (req, res, next) {    console.log("Middleware called")    next();});    // Requests will never reach this routeapp.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:
The project structure will look like this:Make sure you have installed express module using the following command:npm install expressRun index.js file using below command:node index.jsOutput:Server listening on PORT 3000
Now open your browser and go to http://localhost:3000/user and you can see the following output on console as shown below:Server listening on PORT 3000
Middleware called
/user request called
And on browser you will see the Welcome to GeeksforGeeks.
The project structure will look like this:
Make sure you have installed express module using the following command:npm install express
npm install express
Run index.js file using below command:node index.jsOutput:Server listening on PORT 3000

node index.js
Output:
Server listening on PORT 3000

Now open your browser and go to http://localhost:3000/user and you can see the following output on console as shown below:Server listening on PORT 3000
Middleware called
/user request called
And on browser you will see the Welcome to GeeksforGeeks.
Server listening on PORT 3000
Middleware called
/user request called

And on browser you will see the Welcome to GeeksforGeeks.
Express.js
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between dependencies, devDependencies and peerDependencies
Mongoose find() Function
Node.js Export Module
How to connect Node.js with React.js ?
Mongoose Populate() Method
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript