-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
414 lines (355 loc) · 10 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//jshint esversion:6
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const path = require('path');
const crypto = require('crypto');
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require('express-session');
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
//const GoogleStrategy = require('passport-google-oauth20').Strategy;
const findOrCreate = require('mongoose-findorcreate');
const app = express();
var rememberUSER = "";
//
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
// Middleware
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.set('view engine', 'ejs');
// Mongo URI
const mongoURI = 'mongodb://localhost:27017/demo2-DB';
var conn = mongoose.createConnection('mongodb://localhost:27017/demo22-DB');
var conn2 = mongoose.createConnection('mongodb://localhost:27017/chatlistDB');
var foundItems2 = "";
//const mongoURI = 'mongodb+srv://admin-Dorian:[email protected]/test?retryWrites=true&w=majority';
// Create mongo connection
//const conn = mongoose.createConnection(mongoURI, {useNewUrlParser: true});
// Init gfs
let gfs;
conn.once('open', () => {
// Init stream
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
});
// Create storage engine
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
app.use(passport.initialize());
app.use(passport.session());
//mongoose.connect("mongodb+srv://admin-Dorian:[email protected]/<dbname>?retryWrites=true&w=majority", {useNewUrlParser: true});
mongoose.connect("mongodb://localhost:27017/demo2-DB", { useNewUrlParser: true });
mongoose.set("useCreateIndex", true);
//1. setup: DB and its embedded relationship
const itemsSchema = {
name: String
}
const itemsSchema2 = {
name: String,
sender: String,
receiver: String
};
const Item = conn.model("Item", itemsSchema);
const Item2 = conn2.model("Item2", itemsSchema2);
const item1 = new Item({
name: "Welcome to your Personal Note!"
});
const item2 = new Item({
name: "Hit the + button to add a new item."
});
const item3 = new Item({
name: "<-- Hit this to delete an item."
});
const defaultItems = [item1, item2, item3];
const noteSchema = {
name: String,
items: [itemsSchema]
}
const Notes = conn.model("Notes", noteSchema);
const userSchema = new mongoose.Schema({
email: String,
password: String,
items: [itemsSchema]
});
//end of 1.
userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);
const User = conn.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
app.get("/", function (req, res) {
res.render("home");
});
app.get("/login", function (req, res) {
res.render("login");
});
app.get("/register", function (req, res) {
res.render("register");
});
app.get("/notes", function (req, res) {
Item.find({}, function (err, foundItems) {
if (foundItems.length === 0) {
Item.insertMany(defaultItems, function (err) {
if (err) {
console.log(err);
} else {
console.log("Successfully savevd default items to DB.");
}
});
res.redirect("/notes");
} else {
res.render("notes", { listTitle: "Today", newListItems: foundItems, saymyname: rememberUSER });
}
});
});
app.get('/index', (req, res) => {
gfs.files.find().toArray((err, files) => {
// Check if files
if (!files || files.length === 0) {
res.render('index', { files: false });
} else {
files.map(file => {
if (
file.contentType === 'image/jpeg' ||
file.contentType === 'image/png'
) {
file.isImage = true;
} else {
file.isImage = false;
}
});
res.render('index', { files: files });
}
});
});
app.post('/upload', upload.single('file'), (req, res) => {
// res.json({ file: req.file });
res.redirect('/index');
});
// @route GET /files
// @desc Display all files in JSON
app.get('/files', (req, res) => {
gfs.files.find().toArray((err, files) => {
// Check if files
if (!files || files.length === 0) {
return res.status(404).json({
err: 'No files exist'
});
}
// Files exist
return res.json(files);
});
});
// @route GET /files/:filename
// @desc Display single file object
app.get('/files/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists'
});
}
// File exists
return res.json(file);
});
});
// @route GET /image/:filename
// @desc Display Image
app.get('/image/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists'
});
}
// Check if image
if (file.contentType === 'image/jpeg' || file.contentType === 'image/png') {
// Read output to browser
const readstream = gfs.createReadStream(file.filename);
readstream.pipe(res);
} else {
res.status(404).json({
err: 'Not an image'
});
}
});
});
// @route DELETE /files/:id
// @desc Delete file
app.delete('/files/:id', (req, res) => {
gfs.remove({ _id: req.params.id, root: 'uploads' }, (err, gridStore) => {
if (err) {
return res.status(404).json({ err: err });
}
res.redirect('/index');
});
});
app.get("/logout", function (req, res) {
req.logout();
res.redirect("/");
});
app.post("/register", function (req, res) {
User.register({ username: req.body.username }, req.body.password, function (err, user) {
if (err) {
console.log(err);
res.redirect("/register");
} else {
passport.authenticate("local")(req, res, function () {
res.redirect("/notes");
});
}
});
});
app.post("/login", function (req, res) {
const user = new User({
username: req.body.username,
password: req.body.password
});
req.login(user, function (err) {
if (err) {
console.log(err);
} else {
passport.authenticate("local")(req, res, function () {
console.log("hello" + user.username);
rememberUSER = user.username;
res.redirect("/notes");
});
}
});
});
app.post("/notes", function (req, res) {
const itemName = req.body.newItem;
const listName = req.body.list;
const item = new Item({
name: itemName
});
if (listName === "Today") {
item.save();
res.redirect("/notes");
}
// else {
// List.findOne({name: listName}, function(err, foundList){
// foundList.items.push(item);
// foundList.save();
// res.redirect("/notes" + listName);
// });
// }
});
app.post("/delete", function (req, res) {
const checkedItemId = req.body.checkbox;
const listName = req.body.listName;
if (listName === "Today") {
Item.findByIdAndRemove(checkedItemId, function (err) {
if (!err) {
console.log("Successfully deleted checked item.");
res.redirect("/notes");
}
});
} else {
List.findOneAndUpdate({ name: listName }, { $pull: { items: { _id: checkedItemId } } }, function (err, foundList) {
if (!err) {
res.redirect("/notes" + listName);
}
});
}
})
app.get("/receivebox", function (req, res) {
console.log(rememberUSER);
console.log(Item2.find());
Item2.find({ receiver: rememberUSER }, function (err, foundItems2) {
console.log(foundItems2);
if (foundItems2.length === 0) {
const item4 = new Item2({
name: "No new message.",
sender: "SYSTEM",
receiver: rememberUSER
});
const defaultItems2 = [item4];
Item2.insertMany(defaultItems2, function (err) {
if (err) {
console.log("err here" + err);
} else {
console.log("Successfully savevd default items to DB.");
}
});
res.redirect("/receivebox");
} else {
console.log("success in part1");
res.render("receivebox", {
newListItems2: foundItems2,
saymyname: rememberUSER
});
}
});
});
app.post("/receivebox", function (req, res) {
const msgName = req.body.sendMSG;
const receiveName = req.body.sendTO;
console.log(req.body);
var sendername = rememberUSER;
const item2 = new Item2({
name: msgName,
sender: sendername,
receiver: receiveName
});
item2.save();
console.log("Successfully savevd");
res.redirect("/receivebox");
});
app.post("/delete2", function (req, res) {
console.log(req.body);
const checkedItemId = req.body.checkbox;
Item2.findByIdAndRemove(checkedItemId, function (err) {
if (!err) {
console.log("Successfully deleted checked item.");
res.redirect("/receivebox");
}
});
});
app.get("/faces", function (req, res) {
app.engine('html', require('ejs').renderFile);
res.render("face.html");
});
app.get("/rekognition", function (req, res) {
app.engine('html', require('ejs').renderFile);
res.render("rekognition.html");
});
app.listen(3001, function () {
console.log("Server started on port 3001.");
});