-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress_server.js
More file actions
190 lines (172 loc) · 5.25 KB
/
express_server.js
File metadata and controls
190 lines (172 loc) · 5.25 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
const express = require("express");
const bodyParser = require("body-parser");
const cookieSession = require("cookie-session");
const app = express();
const PORT = 8080;
const { checkEmail, getID, generateRandomString } = require('./helpers');
const bcrypt = require('bcrypt');
app.use(cookieSession({
name: 'session',
keys: ["user_id"]
}));
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
//obj with shortnames for testing purpose
const urlDatabase = {
"b2xVn2": { longURL: "http://www.lighthouselabs.ca", userID: "aJ48lW" },
"9sm5xK": { longURL: "http://www.google.com", userID: "test" }
};
//obj with user id, names and emails for testing purpose
const users = {
"userRandomID": {
id: "userRandomID",
email: "user@example.com",
password: bcrypt.hashSync("123", 10)
},
"test": {
id: "test",
email: "test1@t.ca",
password: bcrypt.hashSync("123", 10)
}
};
//function which create a new obj with URL which belong only to certain userID
const urlsForUser = (id, urlDatabase) => {
const ObjUserID = {};
for (shortURL in urlDatabase) {
if (urlDatabase[shortURL].userID === id) {
ObjUserID[shortURL] = urlDatabase[shortURL]
}
}
return ObjUserID;
};
//starter page
app.get("/", (req,res) => {
res.redirect("/u")
});
//index page
app.get("/u", (req, res) => {
let cookiesID = req.session.user_id;
if (cookiesID !== undefined) {
const userURLs = urlsForUser(cookiesID, urlDatabase);
let templateVars = { urls: userURLs, username: users[req.session.user_id] };
res.render("urls_index", templateVars);
} else {
res.redirect("/login")
}
});
//creating userID and short link
app.post("/u", (req, res) => {
let cookiesID = req.session.user_id;
if (cookiesID === undefined) {
res.render("urls_login", { username: req.session.user_id })
} else {
const shortURL = generateRandomString();
const userURLs = urlsForUser(cookiesID, urlDatabase);
const longURL = req.body.longURL;
urlDatabase[shortURL] = { longURL, userID: req.session.user_id };
userURLs[shortURL] = urlDatabase[shortURL];
res.redirect(`/u/`);
}
});
//creatin new short url
app.get("/u/new", (req, res) => {
let cookiesID = req.session.user_id;
if (cookiesID !== undefined) {
let templateVars = { username: users[req.session.user_id] };
res.render("urls_new", templateVars);
} else {
res.redirect("/login");
}
});
//redirection to initial site longURL
app.get("/u/:shortURL", (req, res) => {
const shortURL = req.params.shortURL;
const longURL = urlDatabase[shortURL].longURL;
if (!longURL) {
res.statusCode = 404;
res.send(" Sorry, code 404, Page Not Found");
} else {
res.redirect(longURL);
}
});
//redirection to edit page
app.get("/u/:shortURL/update/", (req, res) => {
let cookiesID = req.session.user_id;
if (cookiesID !== undefined) {
const shortURL = req.params.shortURL;
const longURL = urlDatabase[shortURL].longURL;
const templateVars = { longURL, shortURL, username: users[req.session.user_id] };
res.render("urls_show", templateVars);
} else {
res.redirect("/login");
}
});
//adding edit option on urls_show.ejs
app.post("/u/:shortURL/update", (req, res) => {
let cookiesID = req.session.user_id;
const shortURL = req.params.shortURL;
const longURL = req.body.longURL;
const userURLs = urlsForUser(cookiesID, urlDatabase);
userURLs[shortURL].longURL = longURL;
res.redirect(`/u/`)
});
// adding delete to the urls_index
app.post("/u/:shortURL/delete", (req, res) => {
let cookiesID = req.session.user_id;
if (cookiesID !== undefined) {
const del = req.params.shortURL;
const userURLs = urlsForUser(cookiesID, urlDatabase);
delete urlDatabase[del];
res.redirect("/u");
} else {
res.redirect("/login");
}
});
//adding logout feature in header
app.post("/logout", (req, res) => {
req.session = null;
res.redirect(`/u/`);
});
// adding register page
app.get("/register", (req, res) => {
res.render("urls_register", { username: req.session.user_id })
});
//adding handling registration form
app.post("/register", (req, res) => {
if (checkEmail(req.body.email, users) !== true) {
const id = generateRandomString();
const email = req.body.email;
const password = req.body.password;
const hashedPassword = bcrypt.hashSync(password, 10);
users[id] = { id, email, password: hashedPassword }
req.session.user_id = id;
res.redirect('/u');
} else {
res.statusCode = 400;
res.send(
"Your email is registered already. Proceed to the login page, please. Code 400"
);
}
});
//adding login functionality
app.get("/login", (req, res) => {
const templateVars = { username: req.session.user_id }
res.render("urls_login", templateVars)
});
//adding login feature in header
app.post("/login", (req, res) => {
const email = req.body.email;
const password = req.body.password;
if (getID(email, users) !== false && bcrypt.compareSync(password, users[getID(email, users)].password) === true) {
req.session.user_id = getID(email, users);
res.redirect('/u');
} else {
res.statusCode = 403;
res.send(
"There is no such user or password has not been registered. Code 403"
);
}
});
app.listen(PORT, () => {
console.log(`Tiny app is listening on port ${PORT}!`);
});