Skip to content
Related Articles
Open in App
Not now

Related Articles

What are Request and Cheerio in Node.js NPM ?

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 05 Jan, 2023
Improve Article
Save Article

In this article, we are going to learn about that request and the cheerio library of node.js with their installation.

Request Library:

The request module is a very popular library in node.js. It helps in sending the request to the external web application to make the conversation between the client and the server by using the HTTP request.

HTTP(or HyperText Transfer Protocol) works to send a request and receive a response either in form of JSON or HTML format by parsing to sent request.

It is also useful in establishing communication with the API(Application Programming Interface).

Installation:

npm install request

How to require the request in the Environment:

const request = require('request')

Syntax:

request    (url of web-application, 
    function to handle the response and the error)

Example: Let’s understand with help of an implementation as explained below:

Javascript




const request = require('request');
    function (error, response, html) {
    if (error && response != 200) {
        console.log(error);
    } else {
        console.log(html);
    }
});

Steps to run the application: Write the below code in the terminal to run the application:

node app.js

Output:

HTML result after sending a request

Explanation: First, we sent an HTTP request to an external web application, and then we checked that if there is any error display that error otherwise give us an HTML code in response by passing a function.

Cheerio Library:

Cheerio is a tool to parse the markup language and provides the API to do so many tasks like parsing, manipulation, and rendering efficiently and fast with respect to DOM API. 

Note: Cheerio is a good library and use it as per your requirement but don’t try to compare it with DOM API because DOM is provided by the web browser engines that can do so many tasks that cheerio cannot do like Cheerio cannot interpret a result and it cannot produce any visual rendering that can be done by web browser API that is DOM etc.

Installation:

npm install cheerio

How to require the cheerio in Environment:

const cheerio = require('cheerio)

Syntax:

const selTools = cheerio.load('html code with tags')

Example: Let’s take a few examples to understand with the help of implementation as explained below:

Step 1: First, we will be creating an HTML file as created below:

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Cheerio And Request</title>
    <style>
        .title {
            text-align: center;
        }
 
        ul,
        p {
            font-size: larger;
            font-weight: 400;
        }
    </style>
</head>
 
<body>
    <h1 class="title">Geeks for Geeks</h1>
    <p>Lists of Streams</p>
    <ul id="streams">
        <li id="cse">CSE</li>
        <li id="it">IT</li>
        <li id="me">ME</li>
        <li id="ece">ECE</li>
    </ul>
    <p id="finish">End of the Page</p>
    <script src = "fileName.js">
    </script>
</body>
 
</html>

Step 2: Now we will be creating a javascript having the name fileName.js that has been included in the above HTML for accessing HTML code to manipulate the data with the help of cheerio Library.

Javascript




// Here we have required the install cheerio library
const cheerio = require('cheerio');
 
const selTools = cheerio
    .load('<h1 class="title"> Geeks for Geeks </h1>')
 
selTools('h1.title').text('GFG');
 
selTools('h1').addClass('changed-title');
 
let store = selTools.html();
// Here we have displayed modified html
console.log(store);

Steps to run the application: Write the below code in the terminal to run the application:

node fileName.js

Output:

HTML response as Output

Manipulated HTML with cheerio

Explanation: Initially we loaded the HTML that we wanted to be manipulated and then we used the text method to change the title of the selected HTML then we used the addClass method to show that the title has been changed and Finally we displayed the HTML that we have manipulated to see the result as HTML that we have got after manipulating it.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!