Different types of module used for performing HTTP Request and Response in Node.js
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: 'w3wiki' , 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: