-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
137 lines (117 loc) · 3.39 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
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
datautil = require('./util/datautil'),
commonutils = require('./util/commonutils'),
session = require('express-session'),
cookieParser = require('cookie-parser'),
morgan = require('morgan');
app.use(morgan('dev'));
app.engine('html', require('ejs').renderFile);
app.use(cookieParser());
app.use(session({
secret: commonutils.generateRandomString(64),
name: commonutils.generateRandomString(128),
resave: true,
saveUninitialized: true
}));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
var serveUrls = ['/', '/index', '/home'];
var allowedUrls = ['/login', '/register'];
/**
* Middleware to check if user is authorized for url
*/
app.use('/*', function (req, res, next) {
if (serveUrls.indexOf(req.originalUrl) > -1) {
if (commonutils.isSessionActive(req.session.id))
commonutils.servePage(res, __dirname + '/views/home.html');
else
commonutils.servePage(res, __dirname + '/views/index.html');
}
else if (allowedUrls.indexOf(req.originalUrl) > -1)
next();
else {
if (commonutils.isSessionActive(req.session.id))
next();
else
res.status(403).end();
}
});
/**
* Get all notes for currently logged in user
*/
app.get('/getnoteList', function (req, res) {
datautil.fetchNotes(res, req.session.id);
});
/**
* Register a New User
*/
app.post('/register', function (req, res) {
if (commonutils.isSessionActive(req.session.id)) {
commonutils.redirect(res, '/home');
} else {
var salt = commonutils.generateRandomString(128);
var email = req.body.username;
datautil.addUser(res, {
'name': req.body.name,
'pass': req.body.password,
'salt': salt,
'email': email
});
}
});
/**
* Create a new note with empty content
*/
app.post('/addnote', function (req, res) {
if (req.body.subject != null && req.body.subject.length == 0)
res.send({'success': false, 'message': 'Cannot add an empty Subject'});
else
datautil.addnote(res, req.session.id, req.body.subject, req.body.content);
});
/**
* Update the content of an existing note
*/
app.post('/updatenote', function (req, res) {
datautil.updatenote(res, req.session.id, req.body.note, req.body.content);
});
/**
* Undo from the current note
*/
app.post('/undo', function (req, res) {
datautil.undo(res, req.session.id, req.body.note);
});
/**
* Redo from the current note
*/
app.post('/redo', function (req, res) {
datautil.redo(res, req.session.id, req.body.note);
});
/**
* Logout the user by deleting user session
*/
app.post('/logout', function (req, res) {
commonutils.logoutSession(res, req.session.id);
});
/**
* Check user sent username and password and login the user
*/
app.post('/login', function (req, res) {
if (commonutils.isSessionActive(req.session.id))
res.writeHead(302, {'Location': '/home'});
else
datautil.logInUser(res, {
'email': req.body.email,
'password': req.body.password,
'sessionId': req.session.id
});
});
/**
* Deletes a single task by id
*/
app.post('/deletenote', function (req, res) {
datautil.deletenote(res, req.session.id, req.body.id);
});
app.listen(3000);
module.exports = app;