-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
179 lines (152 loc) · 3.38 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017";
const dbName = 'contacts';
var db = '';
var collection = '';
function connect() {
MongoClient.connect(url, {
useNewUrlParser: true
}, (err, client) => {
if (err) throw err;
console.log("Connected successfully to server");
db = client.db(dbName);
//collection=db.collection('trial');
});
}
app.use(bodyParser.json())
app.use('/', express.static('public'))
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function(req, res) {
res.send("hello world");
})
// app.use('/api',changeendpoints)
app.listen(8080, () => {
console.log("server running on 8080");
connect();
})
app.get('/register', (req, res) => {
res.redirect('/register.html');
})
app.get('/funfacts', (req, res) => {
res.redirect('/funfacts.html');
})
app.get('/dispose', (req, res) => {
res.redirect('/dispose.html');
})
app.get('/don', (req, res) => {
res.redirect('/donater.html');
})
app.get('/contact', (req, res) => {
res.redirect('/contact.html');
})
app.post('/getgc', (req, res) => {
//var data="Paper_Waste";
console.log(typeof(req.body))
// res.sendStatus(200)
getd(req.body.type, function(d) {
console.log(d);
res.json(d)
})
})
app.post('/registergc', (req, res) => {
//console.log(req.body)
var wastetype = req.body.type
//console.log(wastetype)
let {
type,
...form
} = req.body;
//console.log(form)
putdata(wastetype, form, (d) => {
//console.log(d);
res.sendStatus(200);
})
})
app.get('/getss', (req, res) => {
puts((d) => {
console.log(d[0])
res.json(d)
})
})
app.post('/dentry', (req, res) => {
//console.log(req.body)
// var {item,qty,contact,desc}=req.body
//console.log(wastetype)
// console.log(req.body)
donateEntry(req.body, (result) => {
res.json(result[0]);
})
})
app.post('/dupdate', (req, res) => {
var {
id
} = req.body;
update(id, req.body, (d) => {
res.sendStatus(200);
})
})
app.post('/dget', (req, res) => {
//console.log(req.body)
var {
id
} = req.body
//console.log(wastetype)
// console.log(req.body)
getDonateEntry(id, (result) => {
res.json(result[0])
})
})
function update(id, body, cb) {
var collection = db.collection("Donate");
collection.update({
id: id
}, body, function(err, result) {
if (err) throw err;
cb(result)
})
}
function puts(cb) {
var collection = db.collection("Donate");
collection.find().toArray(function(err, result) {
if (err) throw err;
// console.log(result);
cb(result)
});
}
function getd(data, cb) {
let collection = db.collection(data);
collection.find().toArray(function(err, result) {
if (err) throw err;
console.log(result);
cb(result)
});
}
function getDonateEntry(id, cb) {
let collection = db.collection("Donate");
collection.find({
id: id
}).toArray(function(err, result) {
if (err) throw err;
// console.log(result);
cb(result)
});
}
function donateEntry(body, cb) {
let collection = db.collection("Donate");
collection.insertOne(body, (err, result) => {
if (err) throw err;
cb(result)
})
}
function putdata(w, f, cb) {
let collection = db.collection(w);
collection.insertOne(f, (err, result) => {
if (err) throw err;
cb(result)
})
}