-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress_server.js
More file actions
304 lines (275 loc) · 7.94 KB
/
express_server.js
File metadata and controls
304 lines (275 loc) · 7.94 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
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
app.set('view engine', 'ejs');
const cookieSession = require('cookie-session');
const bcrypt = require('bcrypt');
const PORT = 8080;
//helper functions:
const { urlsForUser, generateRandomString, getUserByEmail } = require('./helpers')
//middlewears:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
cookieSession({
name: 'session',
keys: ['key1']
})
);
//database objects:
const urlDatabase = {
//b6UTxQ: { longURL: "https://www.tsn.ca", userId: "aJ48lW" },
//i3BoGr: { longURL: "https://www.google.ca", userId: "aJ48lW" }
};
const users = {
// "userRandomID": {
// id: "userRandomID",
// email: "user@example.com",
// password: "purple-monkey-dinosaur"
// },
// "user2RandomID": {
// id: "user2RandomID",
// email: "user2@example.com",
// password: "dishwasher-funk"
// }
}
//end points/routes:
app.get("/", (req, res) => {
const userId = req.session.user_id;
//if user is logged in, redirect to /urls
if (userId) {
return res.redirect("/urls")
}
//if not logged in, redirect to login
res.redirect('/login')
});
// app.get("/error", (req, res) => {
// const userId = req.session.user_id;
// const templateVars = {
// user: users[userId],
// urls: urlsForUser(userId, urlDatabase),
// };
// res.render("error", templateVars);
// })
app.get("/urls", (req, res) => {
const userId = req.session.user_id;
const templateVars = {
errorMsg: null,
user: users[userId],
urls: urlsForUser(userId, urlDatabase)
};
res.render("urls_index", templateVars);
});
app.get("/urls/new", (req, res) => {
const userId = req.session.user_id;
if (!userId) {
return res.redirect("/login")
}
const templateVars = {
user: users[userId]
};
res.render("urls_new", templateVars);
});
app.get("/register", (req, res) => {
const userId = req.session.user_id;
if (users[userId]) {
return res.redirect("/urls");
}
const templateVars = {
errorMsg: null,
user: null
};
res.render("urls_register", templateVars);
})
app.get("/login", (req, res) => {
const userId = req.session.user_id;
if (users[userId]) {
return res.redirect("/urls");
}
const templateVars = {
errorMsg: null,
user: null
};
res.render("login", templateVars);
})
//for urls ending in a /shortURL, render short and long urls
//as key-value pairs inside template variables object
//and send to be used in urls_show document
app.get("/urls/:shortURL", (req, res) => {
const userId = req.session.user_id;
const shortURL = req.params.shortURL; // grab the thing after the colon above
const thisAccountURLs = urlsForUser(userId, urlDatabase);
//if user is not logged in, redirect to /urls
if (!users[userId]) {
return res.redirect("/urls")
}
//if :shortURL doesn't match anything in url database
if (!urlDatabase[shortURL]) {
const errorMsg = "Oops! Short URL does not exist";
const templateVars = {
errorMsg,
user: users[userId],
urls: urlsForUser(userId, urlDatabase)
};
res.render("urls_index", templateVars);
}
//if :shortURL doesn't match anything in user's account
if (!thisAccountURLs[shortURL]) {
const errorMsg = "You can only access short URLs registered to your account";
const templateVars = {
errorMsg,
user: users[userId],
urls: urlsForUser(userId, urlDatabase)
};
res.render("urls_index", templateVars);
}
let longURL;
if (thisAccountURLs[shortURL]) {
longURL = thisAccountURLs[shortURL].longURL;
}
const templateVars = {
user: users[userId],
shortURL,
longURL
};
res.render("urls_show", templateVars);
});
//tied to href anchor in urls_show url short url link
//upon click, redirects to matching long url
app.get("/u/:shortURL", (req, res) => {
const shortURL = req.params.shortURL;
const userId = req.session.user_id;
//if shortURL doesn't exist, redirect to /urls
if (!urlDatabase[shortURL]) {
const errorMsg = "Oops! Short URL does not exist.";
const templateVars = {
errorMsg,
user: users[userId],
urls: urlsForUser(userId, urlDatabase)
};
res.render("urls_index", templateVars);
//return res.redirect("/urls")
}
const longURL = urlDatabase[shortURL].longURL;
//otherwise, redirect to longURL
res.redirect(longURL);
});
//post handler from urls_new form
//creates a new short url with random string function
//updates database object, includes userId from user_id cookie
app.post("/urls", (req, res) => {
let shortURL = generateRandomString();
let userId = req.session.user_id;
let longURL = req.body.longURL;
//if long URL does not use http://, add it
if (!longURL.includes("http://" || "https://") ) {
longURL = "http://" + longURL
}
urlDatabase[shortURL] = {
longURL,
userId
}
res.redirect(`/urls/${shortURL}`);
});
//endpoint for register form data from urls_register
app.post("/register", (req, res) => {
const email = req.body.email;
const password = req.body.password;
//use bcrypt to hash password
const hashPass = bcrypt.hashSync(password, 10);
//if email already exists in our database, embed error message
if (getUserByEmail(email, users)) {
const errorMsg = "You already have an account with this Email."
const templateVars = {
errorMsg,
user: null
};
res.render("urls_register", templateVars);
return;
}
if (!email || !password) {
const errorMsg = "Please fill out both Email and Password fields."
const templateVars = {
errorMsg,
user: null
};
res.render("urls_register", templateVars);
return;
}
//if all is well, create new user id + user object, add to users database
const userId = generateRandomString();
const userObject = {
id: userId,
email,
password: hashPass
}
users[userId] = userObject;
//set user_id cookie containing id
req.session.user_id = userId;
//redirect user to /urls page
res.redirect("/urls");
});
//login post handler from login.ejs form
app.post("/login", (req, res) => {
const email = req.body.email;
const password = req.body.password;
const userObj = getUserByEmail(email, users);
//if email does not exist in users database, embed error message
if (!userObj) {
const errorMsg = "Email not found."
const templateVars = {
errorMsg,
user: null
};
res.render("login", templateVars);
return;
}
//if hashed password entered doesn't match hashed pass on file
if (!bcrypt.compareSync(password, userObj.password)) {
const errorMsg = "Wrong Password."
const templateVars = {
errorMsg,
user: null
};
res.render("login", templateVars);
return;
}
//if all is well, set cookie and redirect user to /urls
req.session.user_id = userObj.id;
res.redirect("/urls")
});
//handles logout form in _header
//clears user_id cookie, & redirects to '/urls'
app.post("/logout", (req, res) => {
delete req.session.user_id;
res.redirect('/urls');
});
//handles delete button form in urls_index
//deletes key-value pair from database and redirects to /urls page
app.post("/urls/:shortURL/delete", (req, res) => {
//add login check for delete
if (users[req.session.user_id]) {
delete urlDatabase[req.params.shortURL];
}
res.redirect("/urls")
});
//update operation for editing existing shortened URLs
//handles edit button form in urls_show
app.post("/urls/:id", (req, res) => {
//check if user is logged in before allowing an edit
//if not, redirect to /urls
if (!users[req.session.user_id]) {
return res.redirect('/urls')
}
//if user is logged in:
const URLid = req.params.id;
let longURL = req.body.newLongURL;
//if long url does not start with http://, add it
if (!longURL.includes("http://" || "https://") ) {
longURL = "http://" + longURL
}
urlDatabase[URLid].longURL = longURL;
res.redirect('/urls')
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}!`);
});