How to Use Handle Get Request in Express.js ?
Last Updated :
29 Jul, 2024
Express.js is a popular web application framework for Node.js, known for its simplicity and flexibility. One of the fundamental tasks in web development is handling HTTP requests, and GET requests are among the most common. This article will guide you through the process of handling GET requests in Express.js, covering the basics, practical examples, and best practices.
Introduction to GET Requests
A GET request is used to retrieve data from a server. When a client sends a GET request, it is asking the server to fetch a resource without causing any side effects. GET requests are typically used for:
- Fetching web pages
- Retrieving data from APIs
- Downloading files
Syntax:
app.get( path, callback )
Parameters:
- path: It is the path for which the middleware function is being called.
- callback: They can be a middleware function or a series/array of middleware functions.
Note: One of the advantages of using the express.js middleware we can forward the incoming request using next() function present in the every request handling functions of the express.js. Refer this article.
Approach
To handle GET requests in Express.js, create an Express app, define a route using app.get('/path', callback)
, and use the callback to process the request and send a response.
Steps to to Handle GET Request
Step 1: Make a folder structure for the project.
mkdir myapp
Step 2: Navigate to the project directory
cd myapp
Step 3: Initialize the NodeJs project inside the myapp folder.
npm init -y
Step 3: Install the necessary packages/libraries in your project using the following commands.
npm install express
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.19.2",
}
Example: Implementation to show the use handle get request in Express.js.
Node
// app.js
// Importing expresss
const express=require("express")
const app=express();
// Handling get request
app.get("/get",(req,res,next)=>{
res.send("This is the get request");
})
app.get("/get/users",(req,res,next)=>{
res.send("This is the get/users request")
})
app.listen(3000,()=>{
console.log("Server is Running");
})
Step to Run Application: Run the application using the following command from the root directory of the project
node app.js
Output: In your browser type localhost:8000/get you will set below response
Example 2: In this example, we will see how to send the HTML response in the get request.
Node
// app.js
//Importing expresss
const express=require("express")
const app=express();
// Handling get request
app.get("/get",(req,res,next)=>{
// Sending inline html response
res.send("<h1>Get Response</h1>")
})
app.listen(3000,()=>{
console.log("Server is Running");
})
Step to Run Application: Run the application using the following command from the root directory of the project
node app.js
Output: In your browser type localhost:8000/get you will set below response
Similar Reads
How to handle redirects in Express JS? Express JS uses redirects to send users from one URL to another. This can be done for various reasons, such as handling outdated URLs or guiding users through a process. In ExpressJS, you define routes and use them to direct users to the desired URL. It's a way to keep your application organized and
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, UP
2 min read
How to handle URL parameters in Express ? In this article, we will discuss how to handle URL parameters in Express. URL parameters are a way to send any data embedded within the URL sent to the server. In general, it is done in two different ways.Table of ContentUsing queriesUsing Route parameterSteps to Create Express Application:Let's imp
3 min read
How to handle sessions in Express ? ExpressJS is a small framework that works on top of Node web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. It adds helpful utilities to Node HTTP objects and facilitates the render
4 min read
How do you handle nested routes in Express.js? In this article we are going to learn that how can we setup nested routes in Express JS. Nested routes are the routes that are defined within other routes. This is used to organize your code.We are going to implement the nested routes using the below given two approaches.Table of ContentUsing Expres
2 min read
How To Handle Route Parameters in Express? Route parameters in ExpressJS capture dynamic values from URLs, like /users/:userId. These values are accessible in your route handler via req.params, enabling dynamic content generation. This allows for creating reusable routes that handle various inputs with a single pattern.JavaScriptapp.get('/us
4 min read