-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (96 loc) · 3.07 KB
/
server.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
const express = require('express')
const app = express()
const cors = require('cors')
app.use(express.json({ extended: false }))
app.use(cors())
const axios = require('axios')
const keys = require('./config/keys_dev')
const validateForm = require('./validateForm')
const sgMail = require('@sendgrid/mail');
var path = require('path');
app.get('/getCountriesData', (req, res) => {
axios.get('https://corona.lmao.ninja/v2/countries?yesterday&sort')
.then(response => {
res.json(response.data)
})
.catch(err => {
console.log(err)
})
})
app.get('/getall', (req, res) => {
axios.get('https://api.covid19api.com/summary')
.then(data => res.json(data.data))
.catch(err => console.log(err))
})
app.post('/getdaily/:country', (req, res) => {
axios.get(`https://api.covid19api.com/total/dayone/country/${req.params.country}`)
.then(data => {
res.json(data.data)
})
.catch(err => console.log(err))
})
app.post('/symptoms', (req, res) => {
var age = req.body.age
var sex = req.body.sex
var evidence = []
if (req.body.evidence) {
evidence = req.body.evidence
}
let axiosHeaders = {
headers: {
'App_Id': keys.infermedica_app_id,
'App-Key': keys.infermedica_app_key,
'Content-Type': 'application/json'
}
}
var dataString = {
sex,
age,
evidence
}
axios.post('https://api.infermedica.com/covid19/diagnosis', dataString, axiosHeaders)
.then(data => {
if (data.data.should_stop === false) {
res.json(data.data)
} else {
axios.post('https://api.infermedica.com/covid19/triage', dataString, axiosHeaders)
.then(data => {
res.json(data.data)
})
.catch(err => console.log(err))
}
})
.catch(err => console.log(err))
})
app.get('/news', (req, res) => {
axios.get(`https://newsapi.org/v2/everything?q=coronavirus&apiKey=${keys.newsAPI}`)
.then(response => {
res.json(response.data.articles)
})
.catch(err => console.log(err))
})
app.post('/contact', (req, res) => {
const { errors, isValid } = validateForm(req.body)
if (!isValid) {
return res.status(400).json(errors)
}
sgMail.setApiKey(keys.sendGridAPI)
const msg = {
to: '[email protected]',
from: req.body.email,
subject: req.body.name + ' has sent you a message with subject: ' + req.body.subject,
html: `<strong>` + req.body.message + `</strong>`,
}
sgMail.send(msg)
res.json({ success: 'Your message has been sent.' })
})
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}