-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebServer.js
831 lines (746 loc) · 30.3 KB
/
webServer.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* jshint node: true */
/*
* This builds on the webServer of previous projects in that it exports the current
* directory via webserver listing on a hard code (see portno below) port. It also
* establishes a connection to the MongoDB named 'cs142project6'.
*
* To start the webserver run the command:
* node webServer.js
*
* Note that anyone able to connect to localhost:portNo will be able to fetch any file accessible
* to the current user in the current directory or any of its children.
*
* This webServer exports the following URLs:
* / - Returns a text status message. Good for testing web server running.
* /test - (Same as /test/info)
* /test/info - Returns the SchemaInfo object from the database (JSON format). Good
* for testing database connectivity.
* /test/counts - Returns the population counts of the cs142 collections in the database.
* Format is a JSON object with properties being the collection name and
* the values being the counts.
*
* The following URLs need to be changed to fetch there reply values from the database.
* /user/list - Returns an array containing all the User objects from the database.
* (JSON format)
* /user/:id - Returns the User object with the _id of id. (JSON format).
* /photosOfUser/:id' - Returns an array with all the photos of the User (id). Each photo
* should have all the Comments on the Photo (JSON format)
*
*/
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var async = require('async');
var express = require('express');
var app = express();
// Load the Mongoose schema for User, Photo, and SchemaInfo
var User = require('./schema/user.js');
var Photo = require('./schema/photo.js');
var SchemaInfo = require('./schema/schemaInfo.js');
const session = require('express-session');
const bodyParser = require('body-parser');
const multer = require('multer');
const processFormBody = multer({storage: multer.memoryStorage()}).single('uploadedphoto');
const { ContactSupportOutlined } = require('@material-ui/icons');
const fs = require("fs");
// const { FirstPage, LocalConvenienceStoreOutlined, PhotoSharp, ContactsOutlined } = require('@material-ui/icons');
// const { fstat } = require('fs');
// XXX - Your submission should work without this line. Comment out or delete this line for tests and before submission!
//var cs142models = require('./modelData/photoApp.js').cs142models;
// mongoose.connect('mongodb://localhost/cs142project6', { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connect('mongodb://127.0.0.1/cs142project6', { useNewUrlParser: true, useUnifiedTopology: true });
// We have the express static module (http://expressjs.com/en/starter/static-files.html) do all
// the work for us.
app.use(express.static(__dirname));
app.use(session({secret: 'secretKey', resave: false, saveUninitialized: false}));
app.use(bodyParser.json());
app.get('/', function (request, response) {
response.send('Simple web server of files from ' + __dirname);
});
/*
* Use express to handle argument passing in the URL. This .get will cause express
* To accept URLs with /test/<something> and return the something in request.params.p1
* If implement the get as follows:
* /test or /test/info - Return the SchemaInfo object of the database in JSON format. This
* is good for testing connectivity with MongoDB.
* /test/counts - Return an object with the counts of the different collections in JSON format
*/
app.get('/test/:p1', function (request, response) {
// Express parses the ":p1" from the URL and returns it in the request.params objects.
console.log('/test called with param1 = ', request.params.p1);
var param = request.params.p1 || 'info';
if (param === 'info') {
// Fetch the SchemaInfo. There should only one of them. The query of {} will match it.
SchemaInfo.find({}, function (err, info) {
if (err) {
// Query returned an error. We pass it back to the browser with an Internal Service
// Error (500) error code.
console.error('Doing /user/info error:', err);
response.status(500).send(JSON.stringify(err));
return;
}
if (info.length === 0) {
// Query didn't return an error but didn't find the SchemaInfo object - This
// is also an internal error return.
response.status(500).send('Missing SchemaInfo');
return;
}
// We got the object - return it in JSON format.
console.log('SchemaInfo', info[0]);
response.end(JSON.stringify(info[0]));
});
} else if (param === 'counts') {
// In order to return the counts of all the collections we need to do an async
// call to each collections. That is tricky to do so we use the async package
// do the work. We put the collections into array and use async.each to
// do each .count() query.
var collections = [
{name: 'user', collection: User},
{name: 'photo', collection: Photo},
{name: 'schemaInfo', collection: SchemaInfo}
];
async.each(collections, function (col, done_callback) {
col.collection.countDocuments({}, function (err, count) {
col.count = count;
done_callback(err);
});
}, function (err) {
if (err) {
response.status(500).send(JSON.stringify(err));
} else {
var obj = {};
for (var i = 0; i < collections.length; i++) {
obj[collections[i].name] = collections[i].count;
}
response.end(JSON.stringify(obj));
}
});
} else {
// If we know understand the parameter we return a (Bad Parameter) (400) status.
response.status(400).send('Bad param ' + param);
}
});
/*
* URL /user/list - Return all the User object.
*/
app.get('/user/list', function (request, response) {
if (!request.session.login_name) {
//console.error('No user with login' + login_name + 'found', err);
response.status(401).send('Unauthorized');
return;
}
User.find({}, function (err, user) {
if (err) {
console.error('Doing /user/list error:', err);
response.status(400).send(JSON.stringify(err));
return;
}
response.end(JSON.stringify(user));
}).select('_id first_name last_name');
});
app.get('/user/:id', function (request, response) {
if (!request.session.login_name) {
//console.error('No user with login' + login_name + 'found', err);
response.status(401).send('Unauthorized');
return;
}
var id = request.params.id;
User.findOne({ _id: id }, function (err, user) {
if (!user) {
response.status(400).send(JSON.stringify(err));
return;
}
if (err) {
console.error('Doing /user/:id error:', err);
response.status(400).send(JSON.stringify(err));
return;
}
if (id.length === 0) {
response.status(400).send('No user with id ' + id);
return;
}
let jsonText = JSON.stringify(user);
response.end(jsonText);
}).select('_id first_name last_name location description occupation');
});
app.get('/photosOfUser/:id', function (request, response) {
console.log(request.session.user_id)
if (!request.session.login_name) {
//console.error('No user with login' + login_name + 'found', err);
response.status(401).send('Unauthorized');
return;
}
var id = request.params.id;
var photosArray = [];
Photo.find({user_id: id }, function (err, photos) {
console.log(photos);
// photos = photos.filter(photo =>
// photo.viewableUsers.indexOf(request.session.user_id) != -1
// // ||
// // photo.user_id == request.session.user_id
// );
photos = photos.filter(function (photo) {
return (photo.viewableUsers.indexOf(request.session.user_id) != -1 || photo.user_id == request.session.user_id);
// ||
// photo.user_id == request.session.user_id
}
);
console.log(photos);
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
async.eachOf(photos, function (photo, key, done_callback) {
var commentsArray = [];
async.eachOf(photo.comments, function (comment, keyIndex, comment_done_callback) {
User.findOne({ _id: comment.user_id }, function (errVal, user) {
if (errVal) {
console.error('Doing /photosOfUser/:id error:', errVal);
response.status(400).send(JSON.stringify(errVal));
return;
}
if (user) {
comment = JSON.parse(JSON.stringify(comment));
let newCommentObj = {
_id: comment._id,
comment: comment.comment,
date_time: comment.date_time,
user:user
};
comment = newCommentObj;
commentsArray[keyIndex] = comment;
}
comment_done_callback(errVal);
}
).select('_id first_name last_name likes viewableUsers');
}, function (photoErr) {
if (photoErr) {
response.status(400).send(JSON.stringify(photoErr));
} else {
photo = JSON.parse(JSON.stringify(photo));
console.log(photo.viewableUsers);
console.log(photo.viewableUsers.indexOf(request.session.user_id))
// if (photo.viewableUsers.indexOf(request.session.user_id) != -1) {
photosArray[key] = photo;
photosArray[key].comments = commentsArray;
console.log(photosArray);
// }
done_callback(photoErr);
}
});
}, function (lastErr) {
if (lastErr) {
response.status(400).send(JSON.stringify(lastErr));
} else {
response.end(JSON.stringify(photosArray));
}
});
} ).select('_id user_id comments file_name date_time likes viewableUsers');
});
//add app.get request
app.get('/request/session', function (request, response) {
console.log('in session');
console.log(JSON.stringify(request.session));
response.send(JSON.stringify(request.session));
response.end();
});
app.post('/photos/new', function (request, response) {
processFormBody(request, response, function (err) {
let viewableUsersVar = request.body.viewableUsers;
if (!request.body.viewableUsers) {
viewableUsersVar = [];
} else {
console.log('VIEWABLE USERS VAR')
console.log(viewableUsersVar);
console.log(viewableUsersVar.split(","));
viewableUsersVar = viewableUsersVar.split(",");
// viewableUsersVar = JSON.parse(viewableUsersVar);
}
if (err || !request.file) {
console.error('Doing /photos/new error:', err);
response.status(400).send(JSON.stringify(err));
return;
}
if (request.file.fieldname !== 'uploadedphoto') {
response.status(400).send('Bad field name');
return;
}
if (!request.session.login_name) {
//console.error('No user with login' + login_name + 'found', err);
response.status(401).send('Unauthorized');
return;
}
// request.file has the following properties of interest
// fieldname - Should be 'uploadedphoto' since that is what we sent
// originalname: - The name of the file the user uploaded
// mimetype: - The mimetype of the image (e.g. 'image/jpeg', 'image/png')
// buffer: - A node Buffer containing the contents of the file
// size: - The size of the file in bytes
// XXX - Do some validation here.
// We need to create the file in the directory "images" under an unique name. We make
// the original file name unique by adding a unique prefix with a timestamp.
const timestamp = new Date().valueOf();
const filename = 'U' + String(timestamp) + request.file.originalname;
fs.writeFile("./images/" + filename, request.file.buffer, function (err) {
if (err) {
console.error('Doing /photos/new error:', err);
response.status(400).send(JSON.stringify(err));
return;
}
// XXX - Once you have the file written into your images directory under the name
// filename you can create the Photo object in the database
// file_name: String, // Name of a file containing the actual photo (in the directory project6/images).
// date_time: {type: Date, default: Date.now}, // The date and time when the photo was added to the database
// user_id: mongoose.Schema.Types.ObjectId, // The ID of the user who created the photo.
// comments: [commentSchema] // Array of comment objects representing the comments made on this photo.
Photo.create({
user_id: request.session.user_id,
file_name: filename,
date_time: new Date().toISOString(),
comments: [],
likes: [],
viewableUsers: viewableUsersVar,
}, function (err, photo) {
console.log(photo);
if (err) {
console.error('Doing /photos/new error:', err);
response.status(400).send(JSON.stringify(err));
return;
}
photo.save(function (err) {
if (err) {
console.error('Doing /photos/new error:', err);
response.status(400).send(JSON.stringify(err));
return;
}
console.log('Photo created:', photo);
response.end(JSON.stringify(photo));
});
});
});
});
});
app.post('/commentsOfPhoto/:photo_id', function (request, response) {
let newComment = {};
console.log(request.session.user_id)
newComment.user_id = request.session.user_id;
newComment.date_time = new Date();
newComment.comment = request.body.comment;
if (!request.session.login_name) {
//console.error('No user with login' + login_name + 'found', err);
response.status(401).send('Unauthorized');
return;
}
// console.log(newComment);
// console.log(request.params.photo_id)
Photo.findOne({_id: request.params.photo_id }, function (err, photo) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!photo) {
response.status(400).send('No photo with id ' + request.params.photo_id);
return;
}
// console.log(photo.comments);
// console.log(photo)
photo.comments.push(newComment);
console.log(photo._id)
console.log(newComment)
photo.save(function (err) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
response.end(JSON.stringify(photo));
});
});
// console.log(jsonText);
});
app.get('/numberOfLikes/:photo_id', function (request, response) {
Photo.findOne({_id: request.params.photo_id }, function (err, photo) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!photo) {
console.log('NO PHOTO')
response.status(400).send('No photo with id ' + request.params.photo_id);
return;
}
console.log(photo);
if (!photo.likes) {
response.end('0');
}
response.end(JSON.stringify(photo.likes.length));
});
});
app.post('/test', function (request, response) {
response.end('test');
});
app.post('/likePhoto/:photo_id', function (request, response) {
// console.log(request.session.user_id)
if (!request.session.login_name) {
//console.error('No user with login' + login_name + 'found', err);
response.status(401).send('Unauthorized');
return;
} else {
Photo.findOne({_id: request.params.photo_id }, function (err, photo) {
console.log('INSIDE PHOTO FIND')
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!photo) {
response.status(400).send('No photo with id ' + request.params.photo_id);
return;
}
if (!photo.likes) {
console.log('NO ARRAY')
}
// if (!photo.likes) {
// photo.likes = [];
// }
if (photo.likes.indexOf(request.session.user_id) == -1) {
photo.likes.push(request.session.user_id);
}
else {
photo.likes.splice(photo.likes.indexOf(request.session.user_id), 1);
}
// console.log(photo.likes);
photo.save(function (err) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
response.end(JSON.stringify(photo));
});
});
}
// console.log(request.params.photo_id)
// response.end('test end')
});
// app.post('/likePhoto/:photo_id', function (request, response) {
// console.log('asdfasdfasdfasd')
// console.log(request.session.user_id)
// if (!request.session.login_name) {
// //console.error('No user with login' + login_name + 'found', err);
// response.status(401).send('Unauthorized');
// return;
// }
// // console.log(newComment);
// // console.log(request.params.photo_id)
// Photo.findOne({_id: request.params.photo_id }, function (err, photo) {
// if (err) {
// response.status(400).send(JSON.stringify(err));
// return;
// }
// if (!photo) {
// response.status(400).send('No photo with id ' + request.params.photo_id);
// return;
// }
// if (photo.likes.indexOf(request.session.user_id) == -1) {
// photo.likes.push(request.session.user_id);
// }
// else {
// photo.likes.splice(photo.likes.indexOf(request.session.user_id), 1);
// }
// console.log(photo._id)
// // console.log(photo.comments);
// // console.log(photo)
// console.log(photo._id)
// photo.save(function (err) {
// if (err) {
// response.status(400).send(JSON.stringify(err));
// return;
// }
// response.end(JSON.stringify(photo));
// });
// });
// // console.log(jsonText);
// });
app.post('deletePhoto/:photo_id', function (request, response) {
console.log(request.params.photo_id)
Photo.findOne({_id: request.params.photo_id }, function (err, photo) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!photo) {
response.status(400).send('No photo with id ' + request.params.photo_id);
return;
}
photo.remove();
response.end(JSON.stringify(photo));
});
});
// :comment_id
app.post('/deletePhoto', function (request, response) {
photoID = request.body.photo_id;
console.log(photoID);
Photo.findOne({_id: photoID }, function (err, photo) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!photo) {
response.status(400).send('No photo with id ' + request.params.photo_id);
return;
}
photo.comments = [];
photo.remove();
response.end(JSON.stringify(photo));
});
});
app.post('/deleteUser', function (request, response) {
User.findOne({ _id: request.session.user_id }, function (err, user) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!user) {
response.status(400).send('No user with id ' + request.session.user_id);
return;
}
Photo.find({}, function (err, photos) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
for (var i = 0; i < photos.length; i++) {
for (var j = 0; j < photos[i].comments.length; j++) {
if (photos[i].comments[j].user_id == user._id) {
photos[i].comments.splice(j, 1);
photos[i].save();
}
// if (photos[i].likes.length > 0) {
// console.log(photos[i]);
// console.log(photos[i].likes)
// if (photos[i].likes.indexOf(user._id) != -1) {
// photos[i].likes.splice(photos[i].likes.indexOf(user._id), 1);
// }
// console.log(photos[i])
// }
}
if (photos[i].likes.indexOf(user._id) != -1) {
console.log(photos[i])
console.log(photos[i].likes)
photos[i].likes.splice(photos[i].likes.indexOf(user._id), 1);
photos[i].save();
console.log(photos[i].likes)
}
}
// photos.save(function (err) {
// if (err) {
// response.status(400).send(JSON.stringify(err));
// return;
// }
// response.end(JSON.stringify(photos));
// }
// );
});
Photo.deleteMany({ user_id: user._id }, function (err) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
else {
Photo.find({}, function (err, photos) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
for (var i = 0; i < photos.length; i++) {
for (var j = 0; j < photos[i].comments.length; j++) {
if (photos[i].comments[j].user_id == user._id) {
photos[i].comments.splice(j, 1);
photos[i].save();
}
// console.log(photos[i]);
// console.log(photos[i].likes);
if (photos[i].likes.indexOf(user._id) != -1) {
photos[i].likes.splice(photos[i].likes.indexOf(user._id), 1);
photos[i].save();
}
}
}
// photos.save(function (err) {
// if (err) {
// response.status(400).send(JSON.stringify(err));
// return;
// }
// response.end(JSON.stringify(photos));
// }
// );
});
}
});
User.deleteOne({ _id: request.session.user_id }, function (err) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
response.end('User deleted');
}
);
});
});
app.post('/deleteAllComments/', function (request, response) {
photoID = request.body.photo_id;
console.log(request.params.photo_id)
Photo.findOne({ _id: photoID }, function (err, photo) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!photo) {
response.status(400).send('No photo with id ' + photoID);
return;
}
photo.comments = [];
photo.save(function (err) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
response.end(JSON.stringify(photo));
});
});
});
app.get('/userList', function (request, response) {
User.find({}, function (err, users) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
response.end(JSON.stringify(users));
});
});
app.post('/deleteComment/', function (request, response) {
commentID = request.body.comment_id;
photoID = request.body.photo_id;
// console.log(photoID);
Photo.findOne({_id: photoID }, function (err, photo) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (!photo) {
response.status(400).send('No photo with id ' + photoID);
return;
}
for (var i = 0; i < photo.comments.length; i++) {
if (photo.comments[i]._id == commentID) {
photo.comments.splice(i, 1);
break;
}
}
photo.save(function (err) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
});
response.end(JSON.stringify(photo));
});
});
app.post('/admin/login', function (request, response) {
User.findOne({ login_name: request.body.login_name }, function (err, user) {
//console.log(request.body);
// console.log(request.body.login_name);
if (!user) {
//console.log(request.body.login_name);
response.status(400).send('No user with specified login');
//console.log(user);
return;
}
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
if (request.body.login_name === 0) {
response.status(400).send('No user with specified login');
return;
}
if (user.password !== request.body.password) {
response.status(400).send('Wrong password');
return;
}
// if (request.body.login_name && !user) {
// console.log(request.body.login_name);
// //console.error('No user with login' + login_name + 'found', err);
// response.status(400).send('Unauthorized');
// return;
// }
// console.log('user list', user);
request.session.login_name = request.body.login_name;
request.session.user_id = user._id;
response.end(JSON.stringify(user));
})
});
app.post('/user', function (request, response) {
console.log(request.body.first_name)
if (!request.body.first_name || !request.body.last_name || !request.body.login_name || !request.body.password) {
response.status(400).send('Missing required fields');
return;
}
User.find({ login_name: request.body.login_name }, function (err, users) {
console.log(users);
if (err || !users) {
response.status(400).send(JSON.stringify(err));
return;
}
if (users.length > 0) {
response.status(400).send('User with specified username already exists');
return;
}
User.create({
first_name: request.body.first_name,
last_name: request.body.last_name,
login_name: request.body.login_name,
password: request.body.password,
location: request.body.location,
description: request.body.description,
occupation: request.body.occupation,
}, function (err, user) {
if (err) {
response.status(400).send(JSON.stringify(err));
return;
}
user.save(function (err) {
if (err) {
console.error('Doing /user error:', err);
response.status(400).send(JSON.stringify(err));
return;
}
console.log('User created:', user);
response.end(JSON.stringify(user));
});
});
});
});
// User.create(request.body, function (err, user) {
// if (err) {
// response.status(400).send(JSON.stringify(err));
// return;
// }
// response.end(JSON.stringify(user));
// });
// });
app.post('/admin/logout', function (request, response) {
delete request.session.login_name;
delete request.session.user_id;
request.session.destroy(
function (err) {
if (err ) {
console.error('Error destroying session', err);
response.status(400).send(JSON.stringify(err));
return;
}
response.end('Session destroyed');
}
);
});
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Listening at http://localhost:' + port + ' exporting the directory ' + __dirname);
});