Create a COVID-19 Tracker CLI using Node.js
Last Updated :
10 Apr, 2023
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 in the Indian States.
Approach: We use an 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 the 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 the current folder, where the 'app.js' file is located.
The 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.
Example: In this example, we will see the use of command-line Corona Virus Tracker using Node.js
javascript
// Importing the request package
const request = require("request");
// API endpoint to which the http
// request will be made
const url = "https://api.covid19india.org/data.json";
// 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
let 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);
}
})
Output:

Similar Reads
COVID-19 Tracker using ReactJS and real time API Creating a web application COVID-19 Tracker using ReactJS and real-time API. In this web application or website, when the user enters the name of the country, it will display the number of active cases, recovered cases, today's cases, etc.Prerequisites:React JSReactJS HooksIntroduction to APIsAPIs k
3 min read
Health Tracker App Backend Using Node and Express.js A Health Tracker App is a platform that allows users to log and monitor various data of their health and fitness. In this article, we are going to develop a Health Tracker App with Node.js and Express.js. that allows users to track their health-related activities such as exercise, meals, water intak
4 min read
How to Create a CLI Based Calculator App using Node.js ? Creating a Command-Line Interface (CLI) based calculator app using Node.js is a great way to get hands-on experience with Node.js and enhance your understanding of JavaScript in a server-side context. In this article, we will walk through the steps to build a simple yet functional calculator that ca
4 min read
Covid-19 cases update using Cheerio Library In this article we are going to learn about that how can we get the common information from the covid website i.e Total Cases, Recovered, and Deaths using the concept of scraping with help of JavaScript Library. Library Requirements and installation: There are two libraries that are required to scra
3 min read
Cryptocurrency Tracker with Next.js and API The cryptocurrency tracker is a web application built using NextJS that allows users to monitor the prices and other relevant information such as market cap, current price, total supply, and more for your favorite cryptocurrencies. The application provides a user-friendly interface for users to expl
5 min read