-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress_server.js
More file actions
210 lines (184 loc) · 6.07 KB
/
Copy pathexpress_server.js
File metadata and controls
210 lines (184 loc) · 6.07 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
const express = require("express");
const app = express();
const PORT = 8080;
const bodyParser = require("body-parser");
const bcrypt = require('bcryptjs');
const cookieSession = require('cookie-session');
const {getUserByEmail, urlsForUser, generateRandomString, doesTinyURLExist} = require("./helpers.js");
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieSession({
name: 'session',
keys: [ "key1", "key2"],
}));
//DATA
const urlDatabase = {};
const users = {};
app.get("/", (req, res) => {
const templateVars = {user: users[req.session.user_id]};
if (!templateVars.user) {
res.redirect("/urls");
} else {
res.redirect("/login");
}
});
app.get("/urls", (req, res) => {
const templateVars = {
urls: urlsForUser(req.session.user_id, urlDatabase),
user: users[req.session.user_id]
};
res.render("urls_index", templateVars);
});
//ADD NEW URL
app.get("/urls/new", (req, res) => {
const templateVars = {user: users[req.session.user_id]};
if (!templateVars.user) {
res.redirect("/urls");
} else {
res.render("urls_new", templateVars);
}
});
//Generate new shortURL, add to db.
app.post("/urls", (req, res) => {
const templateVars = {user: users[req.session.user_id]};
if (!templateVars.user) {
res.status(401).send("<html><body>Please sign in to shorten a URL</body></html>\n");
} else {
const urlShort = generateRandomString();
const urlLong = req.body.longURL;
urlDatabase[urlShort] = {
longURL: urlLong,
userID: req.session.user_id
};
res.redirect(`/urls/${urlShort}`);
}
});
app.get("/urls/:shortURL", (req, res) => {
const shortURL = req.params.shortURL;
const doesURLExist = doesTinyURLExist(shortURL, urlDatabase);
const yourURLs = urlsForUser(req.session.user_id, urlDatabase);
let templateVars = {};
//templateVars key/values will depend on whether the tinyURL exists.
if (doesURLExist) {
templateVars = {
shortURL,
longURL: urlDatabase[shortURL].longURL,
user: users[req.session.user_id],
doesURLExist,
correctUser: true
};
//if tinyURL not valid
} else {
templateVars = {
doesURLExist,
user: users[req.session.user_id]
};
}
if (!doesTinyURLExist(shortURL, urlDatabase)) {
res.render("urls_show", templateVars);
//if tinyURL not valid, show html page informing user.
} else if (!templateVars.user) {
res.status(401);
res.render("urls_show", templateVars);
//else if user not logged in, show html page informing user.
} else if (req.session.user_id === urlDatabase[shortURL].userID) {
res.status(401);
res.render("urls_show", templateVars);
//else if user is signed in, show html page that that lets them edit the tiny link.
} else if (!Object.keys(yourURLs).includes(shortURL)) {
templateVars.correctUser = false;
res.status(401);
res.render("urls_show", templateVars);
//the tiny code does not belong to user, show html page informing them they cannot edit.
}
});
//Removes existing short URLs from db
app.post("/urls/:shortURL/delete", (req, res) => {
//if user is not logged in, display error message.
const yourURLs = urlsForUser(req.session.user_id, urlDatabase);
const shortURL = req.params.shortURL;
if (!users[req.session.user_id]) {
res.status(401).send("<html><body>Please sign in to perform this action.</body></html>\n");
} else if (!Object.keys(yourURLs).includes(shortURL)) {
res.status(400).send("<html><body>Sorry, this is not your URL to edit. </body></html>\n");
} else {
delete urlDatabase[shortURL];
res.redirect("/urls");
}
});
//Updates URL resource
app.post("/urls/:shortURL", (req, res) => {
const shortURL = req.params.shortURL;
urlDatabase[shortURL].longURL = req.body.longURL;
res.redirect("/urls");
});
//LOGIN
app.get("/login", (req, res) => {
const templateVars = {user: users[req.session.user_id]};
if (templateVars.user) {
res.redirect("/urls");
} else {
res.render("urls_login", templateVars);
}
});
app.post("/login", (req, res) => {
const emailEntered = req.body.email;
const passwordEntered = req.body.password;
const checkDatabase = getUserByEmail(emailEntered, users);
if (!checkDatabase.email) {
res.status(403).send("<html><body> Email not found. </body></html>\n");
} else if (!bcrypt.compareSync(passwordEntered, checkDatabase.password)) {
res.status(403).send("<html><body> Password was incorrect. </body></html>\n");
} else {
const userID = checkDatabase.id;
req.session["user_id"] = userID;
res.redirect("/urls");
}
});
//LOG OUT
app.post("/logout", (req, res) => {
req.session = null;
res.redirect("/urls");
});
//REGISTRATION
app.get("/register", (req, res) => {
const templateVars = {user: users[req.session.user_id]};
if (templateVars.user) {
res.redirect("/urls");
} else {
res.render("urls_registration", templateVars);
}
});
app.post("/register", (req, res) => {
const newEmail = req.body.email;
const newPassword = req.body.password;
const hashedPassword = bcrypt.hashSync(newPassword, 10);
const newID = generateRandomString();
if (!newEmail || !newPassword) {
res.status(400).send("<html><body> Email and/or password cannot be left blank. </body></html>\n");
} else if (getUserByEmail(newEmail, users).email) {
res.status(400).send("<html><body> This email is already registered with an account. </body></html>\n");
} else {
users[newID] = {
id: newID,
email: newEmail,
password: hashedPassword
};
}
req.session["user_id"] = newID;
res.redirect("/urls");
});
//Redirect any request to "/u/:shortURL" to original URL
app.get("/u/:shortURL", (req, res) => {
const shortURLData = urlDatabase[req.params.shortURL];
if(!shortURLData) {
res.status(404).send("<html><body> Sorry, the tiny URL entered was not valid. </body></html>\n")
} else if (shortURLData.longURL.includes("http")) {
res.redirect(shortURLData.longURL);
} else {
res.redirect(`http://${shortURLData.longURL}`);
}
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}!`);
});