Create a COVID-19 Tracker CLI using Node.js

In this article, we will see how to create a command-line Corona Virus Tracker using Node.js. We will track total cases, active cases, totally recovered cases, and total deaths of Indian States.

Approach: We use a npm package called ‘request’ to fetch data from publicly available covid-19 api https://api.covid19india.org/data.json
We will clean the fetched data and print the data using ‘console.table()’ command that will format the data into the table. We can also automate the tracker by scheduling the process using setInterval() method.

request package: The request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

Installing request package:

$ npm install request

Note: Run this command in current folder, where ‘app.js’ file located.



Syntax for request:

request(url, (error, response, body) => {
    if(!error && response.statusCode == 200) {
        statements to be executed.
    }
}

Where,

  • url: API endpoint to which request is made.
  • response: HTTP response status codes indicate whether a specific HTTP request has been successfully completed.
  • body: Response data.

Eexample:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Importing the request package
const request = require("request");
  
// API endpoint to which the http
// request will be made
  
// HTTP request
request(url, (error, response, body) => {
  
    // Error - Any possible error when
    // request is made.
  
    // Eesponse - HTTP response status codes
    // indicate whether a specific HTTP 
    // request has been successfully completed
  
    // body - response data
  
    // 200 - successful response
    if (!error && response.statusCode == 200) {
  
        // The response data will be in string
        // Convert it to Object.
        body = JSON.parse(body);
  
        // The data have lot of extra properties
        // We will filter it
        var data = [];
        for (let i = 0; i < body.statewise.length; i++) {
            data.push({
                "State": body.statewise[i].state,
  
                "Confirmed": body.statewise[i].confirmed,
  
                "Active": body.statewise[i].active,
  
                "Recovered": body.statewise[i].recovered,
  
                "Death": body.statewise[i].deaths
            });
        }
  
        console.log("-----Total Cases in India "
            + "and in each state-----");
  
        // Format to table
        console.table(data);
    }
})

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.