The Wayback Machine - https://web.archive.org/web/20240607113508/https://www.geeksforgeeks.org/difference-between-app-use-and-app-get-in-express-js/
Open In App

Difference between app.use() and app.get() in Express.js

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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...])

Javascript




// Requiring module
const 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 setup
const server = app.listen(8080, function () {
    let port = server.address().port
    console.log("Listening at", port)
})


Run the index.js file using the following command:

node index.js

Output: 

app.get(): This function tells the server what to do when getting requests at a given route.

Index.js

Javascript




// Requiring module
const express = require('express');
const app = express();
 
app.get('/', function (req, res) {
    res.send('Hello Geek');
})
 
// Server setup
const server = app.listen(8080, function () {
    const host = server.address().address
    const 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() method

It 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: 



Previous Article
Next Article

Similar Reads

Difference between app.get() and app.post() in Express.js.
In ExpressJS, app.get() and app.post() are two different methods used to handle HTTP requests but for different purposes. app.get() in ExpressJS:app.get() is used to handle incoming GET requests. GET requests are commonly used for fetching data from the server. For example, when you open a website in your browser, you are making a GET request to re
2 min read
Difference between app-level and route-level middleware in Express
Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next middleware function in the application's request-response cycle. In this article, we will go through app-level and route-level middleware and also see the key differences between them. Table of Content What is App-level Middl
3 min read
Difference Between Express and Fastify Web App Frameworks
While building backend applications with Node JS there is a list of frameworks that can be used with Node JS. Two of the most popular choices are Express.js and Fastify. In this article, we will learn about these frameworks and their difference. Table of Content What is Express JS?Features of Express JS frameworkWhat is Fastify?Features of Fastify
3 min read
Express.js | app.get() Function
The app.get() function returns the value name app setting. The app.set() function is used to assign the setting name to value. This function is used to get the values that are assigned. Syntax: app.get(name) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command.
1 min read
Express app.get() Request Function
The `app.get()` function in Node is designed to route HTTP GET requests to the specified path, associating them with designated callback functions. Its primary purpose is to link middleware to your application, facilitating the handling of GET requests at the specified route. Syntax: app.get( path, callback )Parameters: path: It is the path for whi
2 min read
How to use handle get request in Express.js ?
Introduction: Express.js is the most powerful framework of node.js. It is a routing and Middleware framework for handling the different routing of the webpage, and it works between the request and response cycle. It uses different kinds of middleware functions in order to complete the different requests made by the client for Eg. clients can make g
2 min read
How to use get parameter in Express.js ?
Express Js is a web application framework on top of Node.js web server functionality that reduces the complexity of creating a web server. Express provides routing services i.e., how an application endpoint responds based on the requested route and the HTTP request method (GET, POST, PUT, DELETE, UPDATE, etc). We can create an API endpoint receivin
2 min read
How to use the app object in Express ?
In Node and Express, the app object has an important role as it is used to define routes and middleware, and handling HTTP requests and responses. In this article, we'll explore the various aspects of the `app` object and how it can be effectively used in Express applications. PrerequisitesNode.js and NPM should be installed.Basics of Express JSThe
3 min read
Express app.use() Function
The app.use() function is used to mount the specified middleware function(s) at the path that 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 a
2 min read
How to expire session after 1 min of inactivity in express-session of Express.js ?
In this article, we will see how to expire the session after 1 min of inactivity in express-session of Express.js. Prerequisites Installation of Node.js on WindowsTo set up Node Project in Editor see here. Requires Modules: npm install express npm install express-session Call API: var session = require('express-session') To expire the session after
2 min read