Different types of module used for performing HTTP Request and Response in Node.js
Last Updated :
17 Jun, 2021
HTTP’s requests and responses are the main fundamental block of the World Wide Web. There are many approaches to perform an HTTP request and Response in Node.js. Various open-source libraries are also available for performing any kind of HTTP request and Response.
An HTTP request is meant to either retrieve data from a specified URI or to push data to a server. Between a server and client, it works as a request-response protocol. A client may be the web browser and a server may be the application on a computer system that hosts a website.
There are three approaches to create different post requests are discussed below:
- Using HTTP module
- Using express.js framework
- Using axiom module
HTTP Module: The HTTP module is the built-in module that can be used without external installing command.
Importing Module:
const http = require("http")
Filename: index.js
Javascript
const http = require( "http" )
const server=http.createServer((req,res) => {
if (req.url == '/' ) {
res.write( "<h1>This is the server GFG!<h1>" )
res.statusCode = 200
res.end()
}
})
server.listen((3000),() => {
console.log( "Server is Running" )
})
|
Run index.js file using below command:
node index.js
Output:
Now open your browser and go to http://localhost:3000, you can see the following output:
Express.js Framework: Express.js is a third party module that needs to be installed externally using the npm install command. Express.js is one of the powerful frameworks for Node.js. It can handle different types of client’s requests with the help of different middleware.
Installing Module: Install the module using the following command:
npm install express.js
Filename: index.js
Javascript
const express = require( "express" );
const app = express();
app.get( "/" , (req, res, next) => {
res.send( "unknown request" );
})
app.get( "/GFG" , (req, res, next) => {
res.send( "Getting request of GFG" );
})
app.get( "/Hello" , (req, res, next) => {
res.send( "Getting request of the Hello" );
})
app.listen(3000, () => {
console.log( "Server is Running" );
})
|
Run index.js file using below command:
node index.js
Output:
Server is Running
Now open your browser and go to http://localhost:3000/GFG, you can see the following output:
Axios Module: Another library that can be used is Axios. This is a popular node.js module used to perform HTTP requests and supports all the latest browsers. It also supports async/await syntax for performing a POST request.
Installing Module: Install the module using the following command:
npm install axios
Filename: index.js
Javascript
const axios = require( 'axios' );
const data = {
name: 'geeksforgeeks' ,
job: 'Content Writer' ,
topic: 'Node.js'
};
const addUser = async () => {
try {
const res = await axios.post(URL, data);
console.log( 'Body: ' , res.data);
} catch (err) {
console.error(err.Message);
}
};
|
Run index.js file using below command:
node index.js
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...