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
// Importing http module
const http = require("http")
// Creating http server
const server=http.createServer((req,res) => {
// Handling the request
if(req.url == '/') {
// Sending the response
res.write("<h1>This is the server GFG!<h1>")
res.statusCode = 200
// Ending the response
res.end()
}
})
// Listening the server
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
// Requiring module
const express = require("express");
// Creating express app object
const app = express();
// Handling '/' route
app.get("/", (req, res, next) => {
// Sending the response
res.send("unknown request");
})
// Handling '/GFG' route
app.get("/GFG", (req, res, next) => {
// Sending the response
res.send("Getting request of GFG");
})
// Handling '/Hello' route
app.get("/Hello", (req, res, next) => {
// Sending the response
res.send("Getting request of the Hello");
})
// Server setup
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
// Importing the axios module
const axios = require('axios');
// Data to be sent
const data = {
name: 'geeksforgeeks',
job: 'Content Writer',
topic: 'Node.js'
};
const addUser = async () => {
try {
// Endpoint of resource
var URL = 'https://reqres.in/api/usersdata'
// Making post request
const res = await axios.post(URL, data);
// Printing the response data
console.log('Body: ', res.data);
} catch (err) {
// Printing the error
console.error(err.Message);
}
};
Run index.js file using below command:
node index.js
Output:
Similar Reads
Properties and Methods of Request and Response Objects in Express Request and Response objects both are the callback function parameters and are used for Express and Node. You can get the request query, params, body, headers, and cookies. It can overwrite any value or anything there. However, overwriting headers or cookies will not affect the output back to the br
4 min read
Handling Requests And Responses in Node.js Node.js is a powerful JavaScript runtime for building server-side applications. It provides an efficient way to handle HTTP requests and responses using the built-in http module or frameworks like Express.js.Understanding HTTP Requests and ResponsesAn HTTP request is sent by a client (browser or API
4 min read
Node.js http.ServerResponse.headersSent Property The httpServerResponse.headersSent is an inbuilt application programming interface of class ServerResponse within the HTTP module which is used to check if the header has been sent or not. Syntax: const response.headersSentParameters: This property does not accept any arguments as a parameter. Retur
2 min read
HTTP Request vs HapiJS Request in Node.js Node.js: Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and itâs not a programming language. Most of the people are confused and understand itâs a framework or a programming languag
3 min read
Explain Different Types of HTTP Request Hypertext Transfer Protocol (HTTP) defines a variety of request methods to describe what action is to be done on a certain resource. The most often used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. Let's understand the use cases of HTTP requests: GET: A GET request reads or retrieves
5 min read
Different kinds of HTTP requests HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. These are equivalent to the CRUD operations (create, read, update, and d
5 min read
Node.js http.ServerResponse.connection Method The httpServerResponse.connection is an inbuilt application programming interface of class Server Response within http module which is used to get the response socket of this HTTP connection. Syntax: response.connection Parameters: This method does not accept any argument as a parameter. Return Valu
2 min read
Node.js http.ServerResponse.getHeader() Method The httpServerResponse.getHeader() is an inbuilt application programming interface of the class Server Response within http module which is used to get the response header of the particular name. Syntax: response.getHeader(name) Parameters: This method accepts the name of the header as the parameter
2 min read
Explain the use of req and res objects in Express JS Express JS is used to build RESTful APIs with Node.js. We have a 'req' (request) object in Express JS which is used to represent the incoming HTTP request that consists of data like parameters, query strings, and also the request body. Along with this, we have 'res' (response) which is used to send
4 min read
Node.js http.ServerResponse.end() Method The httpServerResponse.end() is an inbuilt application programming interface of class Server Response within http module which is used to send the signal to the server that all the header has been sent. Syntax: response.end(data, Encodingtype, Callbackfunction) Parameters: This method takes three Pa
2 min read