-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress_server.js
163 lines (152 loc) · 3.9 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
const { urlDatabase, users, emailLookup, loginCheck, urlLookup, moment, morgan, bodyParser, cookieSession, PORT, app, generateRandomString, bcrypt } = require('./helpers.js');
app.set('view engine', 'ejs');
app.listen(PORT, () => {
console.log(`Tiny app listening on port ${PORT}!`);
});
// ALL MIDDLEWARE
app.use(morgan('tiny'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieSession({
name: 'session',
keys: ['key'],
maxAge: 24 * 60 * 60 * 1000 // lasts for 24 hours
}));
app.get("/urls/new", (req, res) => {
const user = users[req.session.user_id];
if (!user) {
res.redirect('/login');
return;
}
res.render("urls_new", { user });
});
// Checks for urls and if valid users are logged in
app.get("/urls/:shortUrl", (req, res) => {
const user = users[req.session.user_id]
const shortUrl = req.params.shortUrl;
if (!user) {
res.sendStatus(400);
return;
}
if (!urlDatabase[shortUrl] || (urlDatabase[shortUrl].user_id !== user.id)) {
res.sendStatus(404);
return;
}
const templateVars = {
...urlDatabase[shortUrl],
user,
};
res.render("urls_show", templateVars);
});
app.post('/urls/:shortUrl', (req, res) => {
const { shortUrl } = req.params;
const user_id = req.session.user_id
if (!urlDatabase[shortUrl]) {
res.sendStatus(404);
return;
}
if (user_id !== urlDatabase[shortUrl].user_id) {
res.sendStatus(403);
return;
}
urlDatabase[shortUrl].longUrl = req.body.longUrl;
res.redirect(`/urls/`)
})
app.get("/u/:shortUrl", (req, res) => {
const shortUrl = req.params.shortUrl
const urlObj = urlDatabase[shortUrl];
if (!urlObj) {
res.sendStatus(404);
return;
}
const longUrl = urlObj.longUrl;
res.redirect(longUrl);
})
// Allows for new users and checks if that email is used yet.
app.get('/register', (req, res) => {
const user = users[req.session.user_id];
res.render('register', { user });
});
app.post('/register', (req, res) => {
const { email, password } = req.body;
const registeredAlready = Boolean(emailLookup(email, users));
if (registeredAlready) {
res.sendStatus(400);
return;
}
const id = generateRandomString;
const hashedPassword = bcrypt.hashSync(password, 10);
users[id] = { id, email, hashedPassword };
req.session.user_id = id;
res.redirect('/urls');
});
// Checks to see valid emails/passwords
app.get('/login', (req, res) => {
const user = users[req.session.user_id];
res.render('login', { user });
});
app.post('/login', (req, res) => {
const { email, password } = req.body;
if (!(email || password)) {
res.sendStatus(403);
return;
}
const user = loginCheck(email, password)
if (!user) {
res.sendStatus(403);
return;
}
req.session.user_id = user.id
res.redirect('/urls');
});
// Displays urls
app.get('/urls', (req, res) => {
const user = users[req.session.user_id];
if (!user) {
res.redirect('/login')
}
const usersUrls = urlLookup(user.id, urlDatabase);
const templateVars = {
usersUrls,
shortenUrlRoute: '/urls/new',
user,
};
res.render('urls_index', templateVars);
});
app.post("/urls", (req, res) => {
const user_id = req.session.user_id;
if (!user_id) {
res.redirect('/login');
return;
}
const id = generateRandomString;
urlDatabase[id] = {
longUrl: req.body.longUrl,
user_id,
id,
time: moment(),
}
res.redirect(`urls/${id}`);
});
app.get("/", (req, res) => {
if (req.session.user_id) {
res.redirect("/urls");
return;
}
res.redirect('/login');
});
// Deletes url
app.post('/urls/:shortUrl/delete', (req, res) => {
const { shortUrl } = req.params;
const user_id = req.session.user_id
if (user_id !== urlDatabase[shortUrl].user_id) {
res.sendStatus(403);
return;
}
delete urlDatabase[shortUrl];
res.redirect('/urls');
});
app.post('/logout', (req, res) => {
req.session.user_id = false;
res.redirect('/urls/new');
});