-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
414 lines (354 loc) · 11 KB
/
server.js
File metadata and controls
414 lines (354 loc) · 11 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const cookieSession = require('cookie-session');
const bcrypt = require('bcrypt');
const app = express();
const PORT = 8080;
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieSession({
name: 'session',
keys: ['dfshjkaf90dsafjk']
}));
const urlDatabase = {
'b2xVn2': {
userID:'userRandomID',
url:'http://www.lighthouselabs.ca',
date: 'Thu Apr 19 2018 21:32:50 GMT-0400 (EDT)',
ipOfVisitors: ['12.345.67.890', '12.345.67.890']
},
'9sm5xK': {
userID:'user2RandomID',
url:'http://www.google.com',
date: 'Thu Apr 19 2018 21:32:50 GMT-0400 (EDT)',
ipOfVisitors: ['12.345.67.890']
},
'5x6rvqp': {
userID:'user3RandomID',
url:'https://wikipedia.org',
date: 'Thu Apr 19 2018 21:32:50 GMT-0400 (EDT)',
ipOfVisitors: ['01.234.56.789', '12.345.67.890', '12.345.67.890']
}
};
const users = {
'userRandomID': {
id: 'userRandomID',
email: 'user@example.com',
password: bcrypt.hashSync('password', 10)
},
'user2RandomID': {
id: 'user2RandomID',
email: 'user2@example.com',
password: bcrypt.hashSync('password', 10)
},
'user3RandomID': {
id: 'user3RandomID',
email: 'example@example.com',
password: bcrypt.hashSync('tests', 10)
}
};
// Retrieves user object from email
function getUser(email) {
const currentUsers = Object.keys(users);
for (const user_id of currentUsers) {
if (users[user_id].email === email) {
return users[user_id];
}
}
return false;
};
// Retrieves short URLs associate with user ID
function urlsForUser(id) {
const urls = {};
const shortURLs = Object.keys(urlDatabase);
for (const shortURL of shortURLs) {
if (urlDatabase[shortURL].userID === id) {
urls[shortURL] = urlDatabase[shortURL];
}
}
return urls;
};
// Gets unique values from array
function getUnique(value, index, element) {
return element.indexOf(value) === index;
};
function validateEmail(email) {
const re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
return re.test(email.toLowerCase());
};
function validateWebsite(longURL) {
const re = /^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$/i;
return re.test(longURL.toLowerCase());
};
function generateRandomStr() {
return Math.random().toString(36).substring(6);
};
/**
* ------------------------------------------------------------------------
* APP JSON DATA
* ------------------------------------------------------------------------
*/
app.get('/urls.json', (req, res) => {
res.json(urlDatabase);
});
/**
* ------------------------------------------------------------------------
* USER REGISTRATION
* ------------------------------------------------------------------------
*/
// If user is logged in, redirects to /urls, otherwise renders registration page
app.get('/register', (req, res) => {
const { user_id } = req.session
if (user_id) {
return res.redirect('/urls');
};
let templateVars = { urls: urlDatabase,
user: users[user_id]
};
return res.render('urls_register');
});
/**
* Makes POST to /register to create new account if user enters valid email and password
*
* Adds new user information to users database, hash password, and sets cookie
*
* Redirects user to /urls upon successful account creation
*/
app.post('/register', (req, res) => {
const { email, password } = req.body;
// Checks for email and password
if (!email || !password) {
return res.status(400).send('<h1>Invalid email or password</h1>');
// Checks if email is already used in database
} else if (getUser(email)) {
return res.status(400).send('<h1>Email already in use</h1>');
// Checks for a valid email
} else if(!validateEmail(email)) {
return res.status(400).send('<h1>Please enter a valid email</h1>');
// Populates new user object
} else {
const newUser = {
id: generateRandomStr(),
email: email,
password: bcrypt.hashSync(password, 10),
}
users[newUser['id']] = newUser;
// Sets cookie
req.session.user_id = newUser.id;
let templateVars = { urls: urlDatabase,
user: users[req.session.user_id] };
return res.redirect('/urls');
};
});
// Clears cookies on logout
app.post('/logout', (req, res) => {
req.session = null;
return res.redirect('/urls');
});
/**
* ------------------------------------------------------------------------
* HOME PAGE
* ------------------------------------------------------------------------
*/
app.get('/' , (req, res) => {
// Redirects to /urls if user is already logged in, otherwise redirects to login page
if (req.session.user_id) {
return res.redirect('/urls');
} else {
return res.redirect('/login');
}
});
/**
* ------------------------------------------------------------------------
* LOGIN FORM
* ------------------------------------------------------------------------
*/
app.get('/login' , (req, res) => {
// Checks for user_id from cookies and redirects to /urls
if (req.session.user_id) {
return res.redirect('/urls');
}
return res.render('login_form');
});
app.post('/login', (req, res) => {
const { email, password } = req.body;
// Retrieves user info object from email, returns false if user does not exist
const user = getUser(email);
// Redirects to /urls upon successful authentication
if (user && bcrypt.compareSync(password, user.password)) {
req.session.user_id = user.id;
return res.redirect('/urls')
} else {
return res.status(403).send('<h1>403: Email or password invalid</h1><a href="/login">Go back</a>');
}
});
/**
* ------------------------------------------------------------------------
* USER CONTROLS
* ------------------------------------------------------------------------
*/
/**
* Displays all short URLs create by user along with:
* - creation date
* - long URL
* - times visited
* - unique visits
*
* Authorised users can delete/edit their short URLs
*
* Users can create new short URLs
*/
app.get('/urls', (req, res) => {
let templateVars = {
user: users[req.session.user_id]
};
// Retrieves all short URLs associated with logged in user
if (req.session.user_id) {
templateVars.urls = urlsForUser(req.session.user_id);
};
return res.render('urls_index', templateVars);
});
app.get('/urls/new', (req, res) => {
// Redirects non-logged-in users to login page
if (!req.session.user_id) {
res.redirect('/login');
} else {
let templateVars = {
urls: urlDatabase,
user: users[req.session.user_id]
};
return res.render('urls_new', templateVars);
}
});
/**
* ------------------------------------------------------------------------
* NEW SHORT URL GENERATOR
* ------------------------------------------------------------------------
*/
app.post('/urls', (req, res) => {
const { longURL } = req.body;
if (!validateWebsite(longURL)) {
return res.status(400).send('Please enter a valid URL');
};
const shortURL = generateRandomStr();
// Adds new shortURL to database
urlDatabase[shortURL] = {
userID: req.session.user_id,
url: longURL,
date: new Date(),
ipOfVisitors: []
};
return res.redirect(`/urls/${shortURL}`);
});
/**
* ------------------------------------------------------------------------
* EDIT SHORT URL PAGE
* ------------------------------------------------------------------------
*/
/**
* Allows user to edit their short URLs and view the following analytics:
* - date created
* - times visited
* - unique visits
*
* Renders HTML error if:
* - short URL does not exist in database
* - user is not logged in
* - user is logged in but is not the user who created the short URL
*/
app.get('/urls/:id', (req, res) => {
const { user_id } = req.session;
const { id } = req.params;
let templateVars = {
shortURL: id,
longURL: '',
user: '',
date: '',
timesVisited: '',
uniqueVisits: '',
invalidURL: false,
noUser: false,
isNotAuthUser: false
};
// Verifies if the short URL code exists in the database
if (!urlDatabase.hasOwnProperty(id)) {
console.log('URL does not exist');
templateVars.invalidURL = true;
return res.render('urls_show', templateVars);
// Checks if user is logged in
} else if (!user_id) {
console.log('User not logged in');
templateVars.noUser = true;
return res.render('urls_show', templateVars);
// Checks if user accessing short URL page is the creator
} else if (urlDatabase[id].userID !== user_id) {
console.log('User does not have access');
templateVars.isNotAuthUser = true;
return res.render('urls_show', templateVars);
} else {
// Gets unique visitors but filtering duplicate IP address entries
const uniqueVisitors = (urlDatabase[id].ipOfVisitors).filter(getUnique);
templateVars.longURL = urlDatabase[id].url;
templateVars.user = users[user_id];
templateVars.date = urlDatabase[id].date;
templateVars.timesVisited = (urlDatabase[id].ipOfVisitors).length;
templateVars.uniqueVisits = uniqueVisitors.length;
}
return res.render('urls_show', templateVars);
});
// POST to update short URL
app.post('/urls/:id', (req, res) => {
const { user_id } = req.session;
const { id } = req.params;
const { longURL } = req.body;
// Checks for valid URL format
if (!validateWebsite(longURL)) {
return res.status(400).send(`<h1>Please enter a valid URL</h1><a href="/urls/${id}">Go back</a>`);
}
// Updates urlDatabase with new values
urlDatabase[id] = {
userID: user_id,
url:longURL,
date: urlDatabase[id].date,
ipOfVisitors: urlDatabase[id].ipOfVisitors
};
return res.redirect(`/urls/${id}`);
});
/**
* ------------------------------------------------------------------------
* REDIRECTION
* ------------------------------------------------------------------------
*/
app.get('/u/:shortURL', (req, res) => {
const { user_id } = req.session;
const { shortURL } = req.params;
// Verifies if shortURL exists in database, otherwise redirect
if (!urlDatabase.hasOwnProperty(shortURL)) {
return res.send('<h1>short URL code does not exist</h1><a href="/">Go back</a>');
};
const longURL = urlDatabase[shortURL].url;
// Logs IP address of user to track unique visits
urlDatabase[shortURL].ipOfVisitors.push(req.ip);
return res.redirect(longURL);
});
/**
* ------------------------------------------------------------------------
* DELETE SHORT URL
* ------------------------------------------------------------------------
*/
app.post('/urls/:id/delete', (req, res) => {
const { user_id } = req.session;
const { id } = req.params;
// Verify user and user's permissions
if (!user_id) {
return res.status(401).send('<h1>401: Unauthorised</h1>');
} else if (urlDatabase[id].userID !== user_id) {
return res.status(401).send('<h1>403: Forbidden</h1>');
} else {
delete urlDatabase[id];
return res.redirect('/urls');
}
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}!`);
});