-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress_server.js
219 lines (189 loc) · 5.57 KB
/
express_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
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
const express = require("express");
const app = express();
const PORT = 8081; // default port 8081
const bodyParser = require("body-parser");
const bcrypt = require('bcrypt');
const saltRounds = 10;
const cookieSession = require('cookie-session');
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(cookieSession({
name: '"session"',
keys: ["key1"],
}));
const users = {
"userRandomID": {
id: "userRandomID",
email: "[email protected]",
password: "purple-monkey-dinosaur"
},
"user2RandomID": {
id: "user2RandomID",
email: "[email protected]",
password: "dishwasher-funk"
}
};
const urlDatabase = {
b6UTxQ: { longURL: "https://www.tsn.ca", userID: "aJ48lW" },
i3BoGr: { longURL: "https://www.google.ca", userID: "aJ48lW" }
};
function generateRandomString() {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for ( let i = 0; i < 6; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function generateRandomID() {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for ( let i = 0; i < 10; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function userLookup (email) {
for (let id in users) {
if (email === users[id].email) {
return users[id];
}
}
}
function urlsForUser(loggedInId){
let dBforUser = {};
for (let short in urlDatabase){
if(loggedInId === urlDatabase[short].userID){
dBforUser[short] = urlDatabase[short];
}
}
return dBforUser;
}
// All the GET Responses:
app.get("/", (req, res) => {
res.redirect("/urls");
});
app.get("/urls.json", (req, res) => {
res.json(urlDatabase);
});
app.get("/u", (req, res) => {
res.json(users);
});
app.get("/hello", (req, res) => {
res.send("<html><body>Hello <b>World</b></body></html>\n");
});
app.get("/urls", (req, res) => {
if (!req.session.userId) {
res.redirect("/register");
} else {
let templateVars = {
urls: urlsForUser(req.session.userId),
user: users[req.session["userId"]],
};
res.render("urls_index", templateVars);
}
});
app.get("/urls/new", (req, res) => {
if (!req.session.userId) {
res.redirect("/register");
} else {
let templateVars = {
shortURL: req.params.shortURL,
longURL: urlDatabase[req.params.shortURL],
user: users[req.session["userId"]],
userId: req.session["userId"],
};
res.render("urls_new", templateVars);
}
});
app.get("/urls/:shortURL", (req, res) => {
if (!req.session.userId) {
res.redirect("/register");
} else {
let templateVars = {
shortURL: req.params.shortURL,
longURL: urlDatabase[req.params.shortURL].longURL,
userId: req.session["userId"],
user: users[req.session["userId"]],
urls: urlDatabase,
};
res.render("urlsShow", templateVars);
}
});
app.get("/u/:shortURL", (req, res) => {
const longURL = urlDatabase[req.params.shortURL].longURL;
res.redirect(longURL);
});
app.get("/register", (req, res) => {
if (req.session.userId) {
return res.redirect("/urls");
}
res.render("registration");
});
// All the POST responses:
app.post("/urls", (req, res) => {
console.log(req.body);
let randString = generateRandomString();
urlDatabase[randString] = {
longURL: req.body.longURL,
userID: req.session.userId
};
res.redirect("/urls/" + randString);
});
app.post("/urls/:shortURL/delete",(req, res) => {
const shortURL = req.params.shortURL;
const userID = req.session.userId;
if (urlDatabase[shortURL].userID === userID){
delete urlDatabase[shortURL];
}
res.redirect("/urls");
});
app.post("/urls/:shortURL/update", (req, res) => {
const shortURL = req.params.shortURL;
const longURL = req.body.longURL;
urlDatabase[shortURL].longURL = longURL;
res.redirect("/urls");
});
app.post("/login",(req, res) => {
let email = req.body.email;
let password = bcrypt.hashSync(req.body.password, saltRounds);
let userDB = userLookup(email);
if(userDB) {
if (bcrypt.compareSync(req.body.password, userDB.password)) {
req.session.userId = userDB.id;
console.log(`user ${email} is logged in`);
res.redirect("/urls");
} else {
res.send("<h1>Ooops, try again!</h1>")
}
} else {
res.send("<h1>Ooops, try again!</h1>")
};
});
app.post("/logOut",(req, res) => {
req.session = null;
res.redirect("/register");
});
app.post("/register",(req, res) => {
let email = req.body.email;
let pWord = req.body.password;
let userDB = userLookup(email)
if(email == '' || pWord == '' || userDB) {
res.status(400).render("registration")
} else {
let id = generateRandomID();
let password = bcrypt.hashSync(req.body.password, saltRounds)
// Adding the user to the DB
let user = { id, email, password };
users[id] = user;
req.session.userId = id;
res.redirect("/urls");
}
});
// Port Listening
app.listen(PORT, () => {
console.log(`TinyApp listening on port ${PORT}!`);
});