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
const request = require( "request" );
request(url, (error, response, body) => {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
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-----" );
console.table(data);
}
})
|
Output:
Share your thoughts in the comments
Please Login to comment...