-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (48 loc) · 1.68 KB
/
server.js
File metadata and controls
55 lines (48 loc) · 1.68 KB
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
//dependencies
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const path = require('path')
const port = 8000
const axios = require('axios')
const urlencodedParser = bodyParser.urlencoded({ extended: false })
const revStr = require('./controllers/reverseStr')
//a function to make an API call - CoWin API
async function fecthData(pincode, date, res) {
let config = {
method: 'GET',
url: `https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=${pincode}&date=${date}`,
header: {
'accept': 'application/json',
'Accept-Language': 'hi_IN'
}
}
await axios(config)
.then((response) => {
if(response.data.centers == 0){
res.render('noMatch.ejs', { out: "No centers found! Try for a broad search..."})
}
else{
res.render('vaccine.ejs', { data: response.data })
}
})
.catch(err => {
res.render('noMatch.ejs', {out: "Hmm...seems like there's a problem. Please check your area PIN or the date."})
})
}
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
app.use(express.static(path.join(__dirname, 'public')))
//defining routes
app.get('/', (req, res) => {
res.render('index.ejs')
})
app.post('/vaccine', urlencodedParser, (req, res) => {
//reversing the YYYY-MM-DD date format
req.body.date = revStr.rev(req.body.date)
//calling fetchData function to make an API call
const data = fecthData(req.body.pincode, req.body.date, res)
})
app.listen(port, () => {
console.log(`Listening on port:${port}`);
})