forked from cs4241-22a/a3-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
238 lines (207 loc) · 6.01 KB
/
server.js
File metadata and controls
238 lines (207 loc) · 6.01 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const express = require("express"),
cookie = require("cookie-session"),
hbs = require("express-handlebars").engine,
app = express(),
bodyP = require("body-parser"),
mongodb = require("mongodb");
var http = require('http');
var enforce = require('express-sslify');
// var fav = require("serve-favicon");
// app.use(fav(""))
const helmet = require("helmet");
app.use(helmet());
app.use(enforce.HTTPS({ trustProtoHeader: true }))
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
// use express.urlencoded to get data sent by defaut form actions
// or GET requests
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(express.json());
const appdata = [];
debugger;
const uri =
"mongodb+srv://poz123:Y7XKfgrMONl1V8Bz@a3.xtfv8bl.mongodb.net/?retryWrites=true&w=majority";
const client = new mongodb.MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let collection = "A3";
client
.connect()
.then(() => {
// will only create collection if it doesn't exist
return client.db("A3").collection("a3");
})
.then((__collection) => {
// store reference to collection
collection = __collection;
// blank query returns all documents
return collection.find({}).toArray();
})
.then(console.log);
// route to get all docs
// app.get( '/', (req,res) => {
// if( collection !== null ) {
// // get array and pass to res.json
// collection.find({ }).toArray().then( result => res.json( result ) )
// }
// })
app.use((req, res, next) => {
if (collection !== null) {
next();
} else {
res.status(503).send();
}
});
// assumes req.body takes form { _id:5d91fb30f3f81b282d7be0dd } etc.
// app.post("/update", (req, res) => {
// collection
// .updateOne(
// { _id: mongodb.ObjectId(req.body._id) },
// { $set: { data: req.body.data } }
// )
// .then((result) => res.json(result));
// });
// we're going to use handlebars, but really all the template
// engines are equally painful. choose your own poison at:
// http://expressjs.com/en/guide/using-template-engines.html
app.engine("handlebars", hbs());
app.set("view engine", "handlebars");
app.set("views", "./views");
// cookie middleware! The keys are used for encryption and should be changed
app.use(
cookie({
name: "session",
keys: ["key1", "key2"],
})
);
app.post("/login", (req, res) => {
// express.urlencoded will put your key value pairs
// into an object, where the key is the name of each
// form field and the value is whatever the user entered
// below is *just a simple authentication example*
// for A3, you should check username / password combos in your database
var query = { name: req.body.username, password: req.body.password };
collection
.find(query)
.toArray()
.then((result) => {
if (result.length >= 1) {
req.session.login = true;
req.session.usr = req.body.username;
res.render("main", {
msg: "Hello " + req.body.username,
layout: false,
});
} else {
// cancel session login in case it was previously set to true
req.session.login = false;
// password incorrect, send back to login page
res.render("index", {
msg: "login failed, please try again",
layout: false,
});
}
});
});
app.post("/register", (req, res) => {
const user = {
name: req.body.username,
password: req.body.password,
data: [],
};
req.session.usr = user.name;
console.log(user);
collection.insertOne(user);
});
app.post("/add", bodyP.json(), (req, res) => {
collection
.find({ name: req.session.usr })
.toArray()
.then((result) => {
let data = result[0].data;
data.push({
first: req.body.data,
second: req.body.data2,
});
collection.updateOne(
{ _id: mongodb.ObjectId(result[0]._id) },
{ $set: { data: data } }
);
res.json(data);
});
});
// assumes only one object to insert
app.post("/delete", bodyP.json(), (req, res) => {
collection
.find({ name: req.session.usr })
.toArray()
.then((result) => {
let data = result[0].data;
if (data[req.body.index]) {
data.splice(req.body.index, 1);
}
collection.updateOne(
{ _id: mongodb.ObjectId(result[0]._id) },
{ $set: { data: data } }
);
res.json(data);
});
});
app.post("/get", (req, res, next) => {
if (collection !== null) {
// get array and pass to res.json
var query = { name: req.body.username, password: req.body.password };
collection
.find(query)
.toArray()
.then((result) => res.json(result));
}
});
app.get("/getRatings", bodyP.json(), (req, res) => {
collection
.find({ name: req.session.usr })
.toArray()
.then((result) => {
let data = result[0].data;
res.json(data);
});
});
app.post("/update", bodyP.json(), (req, res) => {
console.log("update entry called!");
collection
.find({ name: req.session.usr })
.toArray()
.then((result) => {
let data = result[0].data;
data[req.body.index].first = req.body.data;
data[req.body.index].second = req.body.data2;
collection.updateOne(
{ _id: mongodb.ObjectId(result[0]._id) },
{ $set: { data: data } }
);
res.json(data);
});
});
app.get("/", (req, res) => {
res.render("index", { msg: "", layout: false });
});
app.get("/login", (req, res) => {
console.log(req.body.username);
res.render("main", { msg: "Hello " + req.session.usr, layout: false });
});
// add some middleware that always sends unauthenicaetd users to the login page
app.use(function (req, res, next) {
if (req.session.login === true) next();
else
res.render("index", {
msg: "login failed, please try again",
layout: false,
});
});
app.get("/main.html", (req, res) => {
res.render("main", { msg: "success you have logged in", layout: false });
});
app.listen(3000);