forked from cs4241-21a/a3-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_ex.js
193 lines (167 loc) · 5.37 KB
/
server_ex.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const express = require('express'),
mongodb = require('mongodb'),
serveStatic = require('serve-static'),
bodyParser = require('body-parser')
app = express(),
cookie = require('cookie-session'),
dir = 'public/',
port = 3000
ObjectID = require('mongodb').ObjectID,
morgan = require('morgan'),
favicon = require('serve-favicon'),
path = require('path')
require('dotenv').config();
app.use(morgan('dev'));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
const faces = ["(・`ω´・)", ";;w;;", "owo", "UwU", ">w<", "^w^", "(・.◤)", "^̮^", "(>人<)", "( ゚ヮ゚)", "(▰˘◡˘▰)"];
const emojis = ["❤", "🤣", "😍", "😜", "😝", "😬", "🤪", "😳"];
const uri = 'mongodb+srv://' + process.env.DB_USER + ':' + process.env.DB_PASS + '@' + process.env.DB_HOST
const client = new mongodb.MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true })
let db = null;
client.connect()
.then(() => {
// will only create collection if it doesn't exist
return client.db('assignment_3')
})
.then(__db => {
// store reference to collection
db = __db
})
app.use(serveStatic('public'))
app.use(express.urlencoded({ extended: true }))
app.use(cookie({
name: 'session',
keys: ['Lucce', 'Janey']
}))
app.get('/', function (req, res) {
res.sendFile(`${__dirname}/public/login.html`)
});
app.post('/login', async function (req, res) {
let name = req.body.name;
let pass = req.body.pass;
let newUser = false;
let result = await db.collection('users').findOne({ _id: name });
if (!result) {
let obj = {
_id: name,
pass: pass
}
let firstRecord = {
"from": "God",
"to": name,
"fancyFont": false,
"oworeplace": 0,
"message": "Hello! Welcome to the message board!",
"fromowo": owoify("God", 0),
"messageowo": owoify("Hello! Welcome to the message board!", 0)
}
appdata = obj.records;
db.collection("users").insertOne(obj);
db.collection("records").insertOne(firstRecord)
newUser = true;
console.log("new user detected")
}
if (newUser || result.pass === pass) {
req.session.login = true;
req.session.username = name;
res.redirect(`/message_board.html`)
} else {
// password incorrect, redirect back to login page
res.sendFile(__dirname + '/public/login_failed.html')
}
});
app.get('/getMessagesTo', async function (req, res) {
let query = { to: req.session.username };
let appdata = [];
let results = await db.collection("records").find(query);
results.forEach(function (elt, idx) {
appdata.push(elt);
}).then(() => {
res.send(JSON.stringify(appdata))
})
});
app.get('/getMessagesFrom', async function (req, res) {
let query = { from: req.session.username };
let appdata = [];
let results = await db.collection("records").find(query);
results.forEach(function (elt, idx) {
appdata.push(elt);
}).then(() => {
res.send(JSON.stringify(appdata))
})
})
app.post('/submit', bodyParser.json(), async function (req, res) {
let obj = {
from: req.session.username,
to: req.body.name,
fancyFont: req.body.fancyFont,
oworeplace: req.body.checkedRadio,
message: req.body.message,
fromowo: owoify(req.session.username, req.body.checkedRadio),
messageowo: owoify(req.body.message, req.body.checkedRadio)
}
await db.collection('records').insertOne(obj);
res.send(JSON.stringify({
obj: obj,
loggedInUser: req.session.username
}));
});
app.post('/update', bodyParser.json(), async function (req, res) {
const query = {
_id: ObjectID(req.body.dbid)
}
const resultSet = await db.collection('records').findOne(query);
let updateFields;
if (resultSet.message === req.body.message && resultSet.oworeplace === req.body.checkedRadio) {
updateFields = {
$set: {
fancyFont: req.body.fancyFont
}
}
} else {
updateFields = {
$set: {
fancyFont: req.body.fancyFont,
oworeplace: req.body.checkedRadio,
message: req.body.message,
messageowo: owoify(req.body.message, req.body.checkedRadio)
}
}
}
const updateResults = await db.collection('records').updateOne(query, updateFields);
console.log("Number updated: " + updateResults.modifiedCount)
res.send("It worked");
});
app.post('/delete', bodyParser.json(), async function (req, res) {
const query = {
_id: ObjectID(req.body.dbid)
}
const result = await db.collection('records').deleteOne(query);
console.log("Number deleted: " + result.deletedCount);
res.send("wahoo");
});
function owoify(text, expReplace) {
let v = text.replace(/[lr]/g, 'w').replace(/[LR]/g, 'W').replace(/n[aeiou]/g, 'ny').replace(/N[aeiou]/g, 'Ny').replace(/N[AEIOU]/g, 'NY');
let numExclaimations = 0;
switch (expReplace) {
case 0: // text faces
numExclaimations = (v.match(/!/g) || []).length;
for (let i = 0; i < numExclaimations; i++) {
v = v.replace('!', " " + faces[getRandomInt(0, faces.length)] + " ");
}
break;
case 1: // emojis
numExclaimations = (v.match(/!/g) || []).length;
for (let i = 0; i < numExclaimations; i++) {
v = v.replace('!', " " + emojis[getRandomInt(0, emojis.length)] + " ");
}
break;
}
return v;
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
app.listen(process.env.PORT || port)