Difference between app.use() and app.get() in Express.js
Express is a small framework that sits on top of Node.js’s web server functionality to simplify its APIs and add helpful new features
Perquisites: Follow these steps for project setup and module installation.
Step 1: Create a directory using the below-given command. After creating a directory add the location of the created directory in a terminal.
mkdir <folder_name>
Step 2: Now initialize the npm (node package manager) using the below given command.
npm init
Step 3: Now Install Express in the present directory and save it in the dependencies list.
npm install express --save
Code Implementation:
1. app.use(): The app.use() function is used to mount the specified middleware function (are the functions that have access to the request object and response object, or we can call it a response-request cycle) at the path which is being specified. The middleware function is executed when the base of the requested path matches the path.
Syntax:
app.use([path,],callback[,callback...])
index.js
// Requiring moduleconst express = require('express')const app = express() app.use(function(req, res, next) { console.log('hello world') next()}) app.use(function(req, res, next) { console.log('happy holidays') next()}) // Server setupvar server = app.listen(8080, function () { var port = server.address().port console.log("Listening at", port) }) |
Run the index.js file using the following command:
node index.js
Output:

2. app.get(): This function tells the server what to do when get requests at a given route.
index.js
// Requiring modulevar express = require('express');var app = express(); app.get('/', function (req, res) { res.send('Hello Geek');}) // Server setupvar server = app.listen(8080, function () { var host = server.address().address var port = server.address().port console.log(" Listening : ", port)}) |
Run the index.js file using the following command:
node index.js
Output:

Difference between app.use() and app.get() methods: app.use() method app.get() methodIt can be used for making routes modular (like exposing a set of routes from an npm module that other web applications could use). The method is used to expose the GET method. It is intended for binding middleware to your application. The path is a mount path and limits the middleware to only apply any paths requested that begin with it. It is intended for matching and handling a specific route when requested by get http. The middleware function is executed when the base of the requested path matches path. Routes HTTP GET requests to the specified path with the specified callback functions. It will allow for all http requests matching that route. It will only be allowed for http GET requests to that particular route Syntax: app.use([path,],callback[,callback…]) Syntax: app.get(path, callback)
Reference:
- https://www.geeksforgeeks.org/express-js-app-use-function/
- https://www.geeksforgeeks.org/express-js-app-get-function/

