-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
207 lines (193 loc) · 5.42 KB
/
server.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const express = require('express');
const session = require('express-session');
require('dotenv').config();
const compression = require('compression');
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;
const assert = require('assert');
const path = require('path');
const words = require('./src/utils/words.json');
const titleCase = require('./src/utils/titleCase');
const UUID = require('./src/utils/uuid');
const app = express();
const PORT = process.env.PORT || 3000;
const URL = `mongodb://${process.env.MONGOUSER}:${process.env.MONGOPASS}@ds119129.mlab.com:19129/${
process.env.DBNAME
}`;
// app.use('*', (req, res, next) => {
// if (req.headers['x-forwarded-proto'] !== 'https') {
// return res.redirect(`https://${req.headers.host}${req.url}`);
// }
// return next();
// });
const findDocuments = (db, data, cb) => {
const collection = db.collection(process.env.DBNAME);
collection.find({ name: data.board }).toArray((err, docs) => {
assert.equal(err, null);
cb(docs);
});
};
const updateDocuments = (db, data, cb) => {
const collection = db.collection(process.env.DBNAME);
const newData = data;
const id = newData._id;
delete newData._id;
collection.replaceOne({ _id: ObjectId(id) }, newData);
cb();
};
const insertDocument = (db, data, cb) => {
const collection = db.collection(process.env.DBNAME);
collection.insertOne(data);
cb(data);
};
app.use(compression());
app.use(express.static(path.join(__dirname, '/dist')));
app.use(
session({
secret: 'SECRET TOKEN',
cookie: { secure: false, maxAge: 60000 },
resave: false,
saveUninitialized: true,
}),
);
app.get('/:board', (req, res) => {
const searchReq = { board: req.params.board };
const sessData = req.session;
MongoClient.connect(
URL,
{ useNewUrlParser: true },
(err, client) => {
assert.equal(null, err);
const db = client.db(process.env.DBNAME);
findDocuments(db, searchReq, (documents) => {
if (documents.length === 0) {
res.redirect('/');
return;
}
sessData.board = documents[0];
sessData.save();
res.redirect('/');
});
},
);
});
app.get('/', (req, res) => {
res.sendFile('index.html');
});
app.get('/api/searchData', (req, res) => {
const board = req.session.board;
if (board == null) {
res.send({ name: 'Not Found' });
return;
}
res.send(req.session.board);
});
app.get('/api/new', (req, res) => {
const sessData = req.session;
const adj = titleCase(words.adjectives[Math.floor(Math.random() * 100) + 1]);
const adv = titleCase(words.adverbs[Math.floor(Math.random() * 100) + 1]);
const ani = titleCase(words.animals[Math.floor(Math.random() * 100) + 1]);
const boardName = `${adv}${adj}${ani}`;
const searchReq = { board: boardName };
MongoClient.connect(
URL,
{ useNewUrlParser: true },
(err, client) => {
assert.equal(null, err);
const db = client.db(process.env.DBNAME);
findDocuments(db, searchReq, (documents) => {
if (documents.length === 0) {
const id = UUID();
const document = {
name: boardName,
ordered: ['New', 'Complete'],
intro: true,
items: {
New: [
{
id,
title: 'Your First Note',
content: 'I\'m your first note! Feel free to delete me!',
color: 0,
},
],
Complete: [],
},
};
insertDocument(db, document, (data) => {
sessData.board = data;
sessData.save();
res.send(data);
});
} else {
sessData.board = documents[0];
sessData.save();
res.send(documents[0]);
}
});
},
);
});
app.get('/api/search', (req, res) => {
const searchReq = req.query;
const sessData = req.session;
MongoClient.connect(
URL,
{ useNewUrlParser: true },
(err, client) => {
assert.equal(null, err);
const db = client.db(process.env.DBNAME);
findDocuments(db, searchReq, (documents) => {
if (documents.length === 0) {
res.send({ name: 'Not Found' });
return;
}
sessData.board = documents[0];
sessData.save();
res.send(documents[0]);
});
},
);
});
app.get('/api/board', (req, res) => {
const sessData = req.session;
if (sessData.board) {
const board = req.session.baord;
res.send(board);
} else {
const searchReq = req.query;
MongoClient.connect(
URL,
{ useNewUrlParser: true },
(err, client) => {
assert.equal(null, err);
const db = client.db(process.env.DBNAME);
findDocuments(db, searchReq, (documents) => {
sessData.board = documents[0];
res.send(documents[0]);
});
},
);
}
});
app.post('/api/save', (req, res) => {
const board = req.query.board;
MongoClient.connect(
URL,
{ useNewUrlParser: true },
(err, client) => {
assert.equal(null, err);
const db = client.db(process.env.DBNAME);
updateDocuments(db, JSON.parse(board), () => {
res.sendStatus(200);
});
},
);
});
app.get('/api/clearSession', (req, res) => {
req.session.destroy();
res.sendStatus(200);
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}!`); // eslint-disable-line no-console
});