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
//app.js const express = require( 'express' ); const app = express(); const port = 3000; // route for the root endpoint app.get( '/' , (req, res) => { res.send( 'Hello, Postman!' ); }); // route for handling dynamic user IDs app.get( '/users/:userId' , (req, res) => { // Extract the user ID from the URL parameters const userId = req.params.userId; res.send(`Hello, User ${userId}!`); }); // route for handling search with query parameters app.get( '/search' , (req, res) => { // extract the search query from the request's query parameters const query = req.query.q; res.send(`Search Results for : ${query}`); }); // start the Express.js server and listen on the specified port app.listen(port, () => { console.log(`Server is running at http: //localhost:${port}`); }); |
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:
How To Make A GET Request using Postman and Express JS
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
Contact Us