How To Make A GET Request using Postman and Express JS
Last Updated :
14 Dec, 2023
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how To Make A GET Request using Postman and Express JS
Prerequisites
What is GET Request?
The GET Request is a HTTP request method used to get or retrieve the data from the specific resource on the server. The client sends the GET request to the server to retrieve the requested data or resource without affecting the server’s state. Using the GET request, the client can fetch the resources like web pages, images, real-time data, etc.
Steps to make a GET Request using Postman and Express JS
Step 1: In the first step, we will create the new folder as a
a get-request by using the below command in the VS Code terminal.
mkdir get-request
cd get-request
Step 2: After creating the folder, initialize the NPM using the below command. Using this the package.json file will be created.
npm init -y
Step 3: Now, we will install the express dependency for our project using the below command.
npm i express
Step 4: Create a app.js file.
Project Structure:
The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.18.2"
}
Example: Write the following code in the app.js file.
Javascript
const express = require( 'express' );
const app = express();
const port = 3000;
app.get( '/' , (req, res) => {
res.send( 'Hello, Postman!' );
});
app.get( '/users/:userId' , (req, res) => {
const userId = req.params.userId;
res.send(`Hello, User ${userId}!`);
});
app.get( '/search' , (req, res) => {
const query = req.query.q;
res.send(`Search Results for : ${query}`);
});
app.listen(port, () => {
console.log(`Server is running at http:
});
|
Step 5: Start the server by using the below command.
node app.js
Step 6: Now, open the Postman application on the computer.
Step 7: Click on the “New” button and create two new requests. We will name them “Express-GET-Request1” and “Express-GET-Request2“.
Step 8: Now, set the request type as GET, and in the URL section, enter the URL as “http://localhost:3000/users/1
” for Request 1 and “http://localhost:3000/search?q=postman
“ for Request2.
Step 9: Now, we can click on the “Send” button of each request and check the responses in the Postman window.
Output:
Share your thoughts in the comments
Please Login to comment...