-
Notifications
You must be signed in to change notification settings - Fork 0
/
express-server.js
108 lines (90 loc) · 2.62 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
const express = require('express');
const cookieSession = require("cookie-session");
const {
addNewUserToDb,
generateRandomString,
verifyUser,
addCountVisitToUrl,
addVisitInformation } = require("./helper-functions");
const methodOverride = require('method-override');
const { urlDatabase, usersDatabase } = require("./database");
const urlsRouter = require("./routes/urlsRoutes");
const app = express();
const PORT = 8080;
// middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieSession({
name: 'session',
keys: [generateRandomString(10)],
maxAge: 12 * 60 * 60 * 1000
}));
app.use(methodOverride('_method'));
// List of URLS REST Endpoints
app.use('/urls', urlsRouter);
app.set('view engine', 'ejs');
// Home
app.get("/", (req, res) => {
res.render("index", { pageTitle: "tinyapp - tiny url shortener" });
});
// URLS end point
// Tiny URL redirect - Unprotected, everyone can have access
app.get("/u/:id", (req, res) => {
const { userId } = req.session;
const { id } = req.params;
if (!urlDatabase[id]) {
// Not found
res.status(404);
return res.send("This URL can not be found");
}
addCountVisitToUrl(id, urlDatabase);
addVisitInformation(userId, id, urlDatabase);
res.redirect(urlDatabase[id].longUrl);
});
// Authentication - Register, Login and Logout endpoints
app.get("/register", (req, res) => {
const userId = req.session.userId;
if (userId) {
return res.redirect("/urls");
}
res.render("registration", { pageTitle: "tinyapp - Register", user: undefined });
});
app.post("/register", (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
res.status(400);
return res.send("Bad request - Incomplete form");
}
const user = addNewUserToDb(email, password, usersDatabase);
if (!user) {
res.status(400);
return res.send("Bad Request - User already exists");
}
req.session.userId = user.id;
res.redirect("/urls");
});
app.get("/login", (req, res) => {
const userId = req.session.userId;
if (userId) {
return res.redirect("/urls");
}
res.render("login", { pageTitle: "tinyapp - Login", user: undefined });
});
app.post("/login", (req, res) => {
const { email, password } = req.body;
const user = verifyUser(email, password, usersDatabase);
if (!user) {
res.status(403);
return res.send("Invalid credentials");
}
req.session.userId = user.id;
res.redirect("/urls");
});
app.post("/logout", (req, res) => {
req.session = null;
res.clearCookie("userId");
res.redirect("/login");
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});