-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress_server.js
More file actions
253 lines (195 loc) · 5.51 KB
/
Copy pathexpress_server.js
File metadata and controls
253 lines (195 loc) · 5.51 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
const express = require('express');
const methodOverride = require('method-override');
const cookieSession = require('cookie-session');
const bcrypt = require('bcryptjs');
const { reset } = require('nodemon');
const {
generateRandomString,
getUserByEmail,
getCurrentUserByCookie,
displayErrorMsg,
display404ErrorMsg,
display403ErrorMsg,
urlsForUser,
userOwnsURL,
getTimeStamp,
trackVisit
} = require('./helpers');
const { urlDatabase, userDatabase } = require('./data');
const app = express();
const PORT = 8080;
//
// Middleware
//
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
app.use(cookieSession({
name: 'session',
keys: ['secretKey', 'superSecretKey'],
}));
//
// Routes
//
// Login/logout request routes
app.post('/login', (req, res) => {
const user = getUserByEmail(req.body.email);
if (!user || !bcrypt.compareSync(req.body.password, user.password)) {
return display403ErrorMsg(res, 'Invalid login parameters');
}
req.session.userID = user.id;
res.redirect('/urls');
});
app.get('/login', (req, res) => {
const templateVars = {
currentUser: getCurrentUserByCookie(req)
};
if (!getCurrentUserByCookie(req)) {
return res.render('login', templateVars);
}
res.redirect('/urls');
});
app.post('/logout', (req, res) => {
req.session = null;
res.redirect('/login');
});
// Registration requests
app.get('/register', (req, res) => {
const templateVars = {
currentUser: getCurrentUserByCookie(req)
};
if (!getCurrentUserByCookie(req)) {
return res.render('register', templateVars);
}
res.redirect('/urls');
});
app.post('/register', (req, res) => {
const newID = generateRandomString();
const email = req.body.email;
const password = req.body.password;
if (!email || !password || getUserByEmail(email)) {
const errMsg = getUserByEmail(email) ? `Email already in use` : `Email or password entry not valid`;
res.statusCode = 400;
return displayErrorMsg(res, res.statusCode, errMsg, '/register');
}
if (password !== req.body.confirmPassword) {
return display403ErrorMsg(res, 'Passwords do not match', '/register');
}
// Add new user to userDatabase
userDatabase[newID] = {
id: newID,
email,
password: bcrypt.hashSync(password)
};
req.session.userID = newID;
res.redirect('/urls');
});
// Show logged in user list of their urls
app.get('/urls', (req, res) => {
const templateVars = {
currentUser: getCurrentUserByCookie(req),
urls: urlsForUser(req.session.userID)
};
if (!getCurrentUserByCookie(req)) {
return res.redirect('/login');
}
res.render('urls_index', templateVars);
});
// Create new tiny URL
app.put('/urls', (req, res) => {
const urlID = generateRandomString();
if (!getCurrentUserByCookie(req)) {
return display403ErrorMsg(res);
}
if (!req.body.longURL) {
return display404ErrorMsg(res, 'Please enter a URL', '/urls/new');
}
// Add the new url to the urlDatabase
urlDatabase[urlID] = {
longURL: req.body.longURL,
userID: req.session.userID,
dateCreated: getTimeStamp(),
timesVisited: 0,
uniqueVisitors: 0,
visits: []
};
res.redirect(`/urls/${urlID}`);
});
app.get('/urls/new', (req, res) => {
const templateVars = {
currentUser: getCurrentUserByCookie(req),
};
if (!getCurrentUserByCookie(req)) {
return res.redirect('/login');
}
res.render('urls_new', templateVars);
});
// Take user to details page about their short URL
app.get('/urls/:id', (req, res) => {
const reqID = req.params.id;
const urlData = urlDatabase[reqID];
if (!getCurrentUserByCookie(req)) {
return display403ErrorMsg(res);
}
if (!urlData || urlData === undefined) {
return display404ErrorMsg(res);
}
if (!userOwnsURL(req.session.userID, reqID)) {
return display403ErrorMsg(res, 'Unable to edit other users\' URLs', '/urls');
}
const templateVars = {
currentUser: getCurrentUserByCookie(req),
id: reqID,
urlData,
};
res.render('urls_show', templateVars);
});
// Manage POST requests for Edit button
app.put('/urls/:id', (req, res) => {
const reqID = req.params.id;
if (!getCurrentUserByCookie(req)) {
return display403ErrorMsg(res);
}
if (!urlDatabase[reqID]) {
return display404ErrorMsg(res);
}
if (!userOwnsURL(req.session.userID, reqID)) {
return display403ErrorMsg(res, 'Unable to edit other users\' URLs', '/urls');
}
urlDatabase[reqID].longURL = req.body.newLongURL;
res.redirect(`/urls`);
});
// Manage POST requests for the Delete button
app.delete('/urls/:id/delete', (req, res) => {
if (!getCurrentUserByCookie(req)) {
return display403ErrorMsg(res);
}
if (!urlDatabase[req.params.id]) {
return display404ErrorMsg(res);
}
if (!userOwnsURL(req.session.userID, req.params.id)) {
return display403ErrorMsg(res, 'Unable to delete other users\' URLs', '/urls');
}
delete urlDatabase[req.params.id];
res.redirect('/urls');
});
// Take anybody to the longURL's page
app.get('/u/:id', (req, res) => {
const urlData = urlDatabase[req.params.id];
if (!urlData) {
return display404ErrorMsg(res);
}
urlData.uniqueVisitors = (req.session.views || 0) + 1;
urlData.timesVisited += 1;
urlData.visits.push(trackVisit());
res.redirect(urlDatabase[req.params.id].longURL);
});
app.get('/', (req, res) => {
if (!getCurrentUserByCookie(req)) {
return res.redirect('/login');
}
res.redirect('/urls');
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});