-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (114 loc) · 3.51 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const express = require('express')
const app = express()
const request = require('request')
const ipl = require('ip-locator')
// default route
app.get('/',(req, res, next) => {
var myip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
ipl.getDomainOrIPDetails(myip,'json', function (err, data) {
var countryCode = data.countryCode
request('https://api.covid19api.com/total/country/'+countryCode, function (error, response, body) {
try {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body.toString())
res.json(data[data.length-1])
}else{
res.json(error)
}
} catch (error) {
res.json(error)
}
})
})
})
// country code route
app.get('/country/:code',(req, res, next) => {
var countryCode = req.params.code
request('https://api.covid19api.com/total/country/'+countryCode, function (error, response, body) {
try {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body.toString())
res.json(data[data.length-1])
}else{
res.json({"status": false, "code": response.statusCode, "message": "Not found"})
}
} catch (error) {
res.json(error)
}
})
})
//summary
app.get('/summary', (req, res, next) => {
try {
request('https://api.covid19api.com/summary', function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body.toString())
res.json(data)
}else{
res.json({"status": false, "code": response.statusCode, "message": "Not found"})
}
})
} catch (error) {
res.json(error)
}
})
// country list route
app.get('/countries',(req, res, next) => {
request('https://api.covid19api.com/countries', function (error, response, body) {
try {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body.toString())
res.json(data)
}else{
res.json({"status": false, "code": response.statusCode, "message": "Not found"})
}
} catch (error) {
res.json(error)
}
})
})
// day by day
app.get('/days/:code',(req, res, next) => {
var countryCode = req.params.code
var isCumulative = req.query.cumulative
request('https://api.covid19api.com/total/dayone/country/'+countryCode, function (error, response, body) {
try {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body.toString())
// processing data
if(!isCumulative || isCumulative=="false"){
let confirmedCount = data[0].Confirmed
let deathCount = data[0].Deaths
let recoveredCount = data[0].Recovered
for(let i=1; i<data.length; i++){
data[i].Confirmed -= confirmedCount
confirmedCount += data[i].Confirmed
data[i].Deaths -= deathCount
deathCount += data[i].Deaths
data[i].Recovered -= recoveredCount
recoveredCount += data[i].Recovered
}
}
res.json(data)
}else{
res.json({"status": false, "code": response.statusCode, "message": "Not found"})
}
} catch (error) {
res.json(error)
}
})
})
// handling invalid routes
app.get('*', (req, res)=>{
res.json({"status": 404, "message":"Route not found"})
})
// error handling middleware
app.use((e, req, res, next) => {
res.status(422).send({
status: false,
message: e.message
});
})
app.listen(process.env.PORT || 3000, ()=>{
console.log("listening for requests")
})