forked from ksheeraj1161/CoviSafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (120 loc) · 3.53 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require('express');
const app = express();
const bodyParser = require("body-parser");
const {MongoClient}= require('mongodb');
const uri = "mongodb+srv://user:newuser1234@cluster0.f2emn.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
app.use('/', express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({
extended:true
}));
app.set('view engine', 'ejs');
var results, len, id,obj;
async function main(collection) {
const client = new MongoClient(uri);
try {
await client.connect();
const cursor = client.db("CoviSafe").collection(collection).find();
results = await cursor.toArray();
len = results.length;
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
async function createListing(newListing, collection) {
// See https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#insertOne for the insertOne() docs
const client = new MongoClient(uri);
try {
await client.connect();
const cursor = client.db("CoviSafe").collection(collection).find();
results = await cursor.toArray();
console.log(results.length);
newListing._id = results.length;
const result = await client.db("CoviSafe").collection(collection).insertOne(newListing);
console.log(`New listing created with the following id: ${result.insertedId}`);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
app.get('/', async function(req, res) {
await main('Vaccination_Today');
res.render(__dirname+'/views/home.ejs',{
array: results,
size: len
});
})
app.get('/Covid-19', (req, res) => {
res.sendFile(__dirname + '/views/covid.html');
})
app.get('/Statistics', (req, res) => {
res.sendFile(__dirname + '/views/statistics.html');
})
app.get('/Vaccination', (req, res) => {
res.sendFile(__dirname + '/views/vaccination.html');
})
app.get('/Donations', async(req, res) => {
await main('NGOs').catch(console.error);
res.render(__dirname+'/views/donation.ejs',{
array: results,
size: len
});
})
app.get('/Emergency-Contacts', (req, res) => {
res.sendFile(__dirname + '/views/emergency.html');
})
app.get('/Contact-Us', (req, res) => {
res.sendFile(__dirname + '/views/contactus.html');
})
obj={
"_id":0,
"NGO":"",
"Name":"",
"Age":"",
"DOB":"",
"Email":"",
"Address":"",
"Zipcode":"",
"Phone":""
};
var ngo;
app.get('/volunteer/:a',async(req,res)=>{
console.log(req.params.a);
res.render(__dirname+'/views/form.ejs',{
name:req.params.a
});
ngo=req.params.a;
})
app.post('/submit',async(req,res)=>{
console.log(req.body);
obj._id=id;
obj.NGO=ngo;
obj.Name=req.body.Name;
obj.Age=req.body.Age;
obj.DOB=req.body.DOB;
obj.Address=req.body.Address;
obj.Phone=req.body.Phone;
obj.Zipcode=req.body.Zipcode;
obj.Email=req.body.Email;
await createListing(obj,'Volunteers');
res.sendFile(__dirname+'/views/submit.html');
})
var response={
"_id":0,
"Name":"",
"Feedback":"",
"Additional_info":""
}
app.post('/confirm',async(req,res)=>{
console.log(req.body);
response._id=id;
response.Name=req.body.Name;
response.Email=req.body.Email;
response.Feedback=req.body.Feedback;
response.Additional_info=req.body.Additional_info;
createListing(response,'Responses');
res.sendFile(__dirname+'/views/submit.html');
})
app.listen(process.env.PORT || 5000)