Skip to content

Commit fd004c0

Browse files
committed
JSON return added. Use '?format=json' to get it.
1 parent 9c99ebe commit fd004c0

File tree

5 files changed

+1355
-2
lines changed

5 files changed

+1355
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.DS_Store

app.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,49 @@ const app = express();
33

44
const port = process.env.PORT || 3001;
55

6-
const { getCountryTable } = require('./lib/byCountry');
6+
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
77
const { getCompleteTable } = require('./lib/corona');
88

99
app.get('/', (req, res) => {
10+
const format = req.query.format ? req.query.format : '';
11+
12+
if (format.toLowerCase() === 'json') {
13+
return getJSONData().then(result => {
14+
return res.json(result);
15+
}).catch(error => res.send(error));
16+
}
17+
1018
return getCompleteTable().then(result => {
1119
return res.send(result);
1220
}).catch(error => res.send(error));
1321
});
1422

1523
app.get('/:country', (req, res) => {
16-
let { country } = req.params;
24+
const { country } = req.params;
25+
const format = req.query.format ? req.query.format : '';
26+
1727
if (!country || country === 'all') {
28+
if (format.toLowerCase() === 'json') {
29+
return getJSONData().then(result => {
30+
return res.json(result);
31+
}).catch(error => res.send(error));
32+
}
33+
1834
return getCompleteTable().then(result => {
1935
return res.send(result);
2036
}).catch(error => res.send(error));
2137
}
38+
39+
if (format.toLowerCase() === 'json') {
40+
return getJSONDataForCountry(country).then(result => {
41+
return res.json(result);
42+
}).catch(error => res.send(error));
43+
}
44+
2245
return getCountryTable(country).then(result => {
2346
return res.send(result);
2447
}).catch(error => res.send(error));
2548
});
2649

50+
2751
app.listen(port, () => console.log(`Running on ${port}`));

lib/byCountry.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ function extraStats(dataArr) {
6161
);
6262
}
6363

64+
exports.getJSONData = async () => {
65+
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
66+
return result.data;
67+
}
68+
69+
exports.getJSONDataForCountry = async (country) => {
70+
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
71+
const { latest, confirmed, deaths, recovered } = result.data;
72+
const countryData = getDataByState(confirmed, deaths, recovered, country)
73+
.filter(obj => obj.country === country);
74+
return countryData;
75+
}
76+
6477
exports.getCountryTable = async (country) => {
6578
const head = [
6679
'',

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"corona": "./bin/index.js"
88
},
99
"scripts": {
10+
"dev": "nodemon app.js",
1011
"start": "node app.js",
1112
"test": "echo \"Error: no test specified\" && exit 1"
1213
},
@@ -25,5 +26,8 @@
2526
"humanize-number": "0.0.2",
2627
"lodash": "^4.17.15",
2728
"moment": "^2.24.0"
29+
},
30+
"devDependencies": {
31+
"nodemon": "^2.0.2"
2832
}
2933
}

0 commit comments

Comments
 (0)