-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (38 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const cheerio = require('cheerio');
const axios = require('axios');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
const PORT = process.env.PORT || 5000;
const url = 'https://www.worldometers.info/coronavirus/';
const fetchData = async () => {
const result = await axios.default.get(url);
return cheerio.load(result.data);
}
const covidData = [];
const getData = async () => {
const $ = await fetchData();
$('#main_table_countries_today > tbody > tr').each((index, element) => {
// console.log($($(element).find('td')[0]).text());
const head = $(element).find('td');
const country = $(head[0]).text().trim();
const totalCases = $(head[1]).text().trim();
const newCases = $(head[2]).text().trim();
const totalDeaths = $(head[3]).text().trim();
const newDeaths = $(head[4]).text().trim();
const totalRecovered = $(head[5]).text().trim();
const activeCases = $(head[6]).text().trim();
const critical = $(head[7]).text().trim();
const caseRow = { country, totalCases, newCases, totalDeaths, newDeaths, totalRecovered, activeCases, critical };
covidData.push(caseRow);
});
}
app.listen(PORT, () => {
console.log('LISTENING AT PORT : ', PORT);
});
app.get('/',async (req, res) => {
await getData();
console.log(covidData);
res.send(covidData);
})