Node.js is one of the most popular front end frameworks, is heavily used in the industry to create server-based applications. These applications, in turn, helps in serving various data and information to the user. But how exactly does the user relay his requirements to the server? Well, this is done using the HTTP Requests which helps the Node.js developers to ease the entire process. Through the medium of this article on Node.js Requests, I will give you a complete walkthrough of Node.js requests and how to around them.
Below are the topics which I will be covering in this article:
Let’s start off with the very first topic of this article and understand what are Node.js Requests.
What is HTTP Request in Node?
In Node.js, an HTTP Request is basically a message requested by the client to a server over the HTTP i.e hypertext transfer protocol. You can think it of as a packet of information that is sent between the two computers as a primary set of data. This request is then processed by the server and another packet of information is sent back to the client. This packet contains all the necessary data requested by the client and is known as the response. In this article, we will be mostly focussing on the request part.
So let’s understand an HTTP Request in more granular terms. An HTTP request is the mode of browser for transferring data between a client and a web server. This data can be anything including an API, a website, an image, text, etc.
An HTTP Request is usually made up of the following:
- Request Line
- Headers [0 or more]
- Body of the Request [optional]
There are various modules using which you can make the HTTP Requests and process the server requests in the web server space itself. In this Node.js Requests article, I will be talking about a few of them which are heavily used in the industry.
Making Node.js Requests
Below I have listed down the 3 most popular ways to make an HTTP request:
- HTTP Module
- Request Module
- AXIOS
So, let us dig a little deeper into each of these, starting off with the HTTP Module.
Here, I’ll be making Node.js requests by performing queries to a “fake” API: the JSON Placeholder API for everyone’s convenience.
1. HTTP Module
Generally, beginners in Node.js tend to use http.get and https.get while trying to request or GET something from the API. The main advantage of this module is, that it is native to the API, thus it reduces the need for installing any 3rd party. This makes the process a little faster. Below I have written a code which requests data using an HTTP module.
const https = require("https"); const url = "https://my-json-server.typicode.com/edurekaDemo/noderequest/db"; https.get(url, res => { res.setEncoding("utf8"); let body = ""; res.on("data", data => { body += data; }); res.on("end", () => { body = JSON.parse(body); console.log(body); }); });
This will give you the requested data in the console. Below I have attached the output of my code.
In the next section of this Node.js Requests article, I will be talking about the request module which is nothing but an advanced or modified version of the HTTP module.
2. Request Module
Below I have written a code which requests data using the request module. But before you make a request using this module, you need to install it in your systems using the following command:
npm i request
Once you have successfully installed the request module, you can proceed with the below-given example.
const request = require("request"); const url = "https://my-json-server.typicode.com/edurekaDemo/noderequest/db"; request.get(url, (error, response, body) => { let json = JSON.parse(body); console.log(body); });
This will give you the requested data in the console. Below I have attached the output of my code.
But again, while it makes the request process easier it doesn’t have any support for promises and has a lot of dependencies. This leads us to our third way of making Node.js request i.e using axios package.
3. Axios Module
Below I have written a code which requests data using the request module. But before you make a request using this module, you need to install it in your systems using the following command:
npm i axios
Once you have successfully installed the request module, you can proceed with the below-given example.
const axios = require("axios"); const url = "https://my-json-server.typicode.com/edurekaDemo/noderequest/db"; const getData = async url => { try { const response = await axios.get(url); const data = response.data; console.log(data); } catch (error) { console.log(error); } }; getData(url);
This will give you the requested data in the console. Below I have attached the output of my code.
Node.js Request Object
The Request object in Node.js helps in retrieving the values that the client browser has passed to the Node.js server over an HTTP request. It holds the information from the user and assesses in creating a web page dynamically. Based on user inputs it is also capable of performing various server-side operations. You can say, the request object is used to retrieve information about the current HTTP request such as URL, request header, and data, etc. Below I have listed down the most frequently used properties of the request object.
Property | Description |
req.app | This helps in holding a reference to the current instance of the express application using the middleware |
req.baseUrl | This is used for the URL path on which a router instance was mounted |
req.body | This help in holding key-value pairs of data submitted in the request body by the client. |
req.params | This is used for an object holding the properties that are mapped with the route labeled as “parameters” |
req.path | This is used for holding the path of the request URL |
req.protocol | This is used for a request protocol string like “http” or “https” when it is requested with TLS |
req.secure | This is just a Boolean which returns true if a TLS connection is successfully established |
With this, I would like to conclude this article on Node.js Requests. I tried my best to keep the content crisp and clear for your ease of understanding. If this has perked up your interest in knowing more about Node.js, you can refer to my other articles on Node.js as well.
If you found this “Node.js Requests” relevant, check out the Node.js Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of this Node.js Requests and we will get back to you.