-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (96 loc) · 2.45 KB
/
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
const express = require("express");
const app = express();
const port = process.env.PORT || 4000;
const bodyParser = require("body-parser");
const slug = require("slug");
const getUserData = require("./src/js/database");
const dbName = "Project-Tech";
app.set("view engine", "ejs");
app.set("views", "views");
app.use(express.static("src"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use((err, req, res, next) => {
res.status(404).send("404 not found");
});
app.get("/", (req, res) => {
res.render("about", {
pageTitle: `about`,
});
});
app.get("/login", (req, res) => {
res.render("login", {
pageTitle: `log-in`,
});
});
app.get("/signup", (req, res) => {
res.render("signup", {
pageTitle: `sign-up`,
});
});
app.get("/profile/:id", (req, res) => {
getUserData(dbName)
.then((user) =>
user.findOne({
username: req.params.id,
})
)
.then((foundUser) =>
res.render("profile", {
data: foundUser,
pageTitle: `profile`,
})
);
});
app.get("/error/:id", (req, res) => {
req.params.id === "email"
? res.render("error", {
data: "De gekozen e-mail adres is al in gebruik",
pageTitle: `error`,
})
: res.render("error", {
data: "Gebruiker niet gevonden",
pageTitle: `error`,
});
});
app.post("/login", checkForUser);
app.post("/signup", createUser);
app.listen(port, function () {
console.log(`Application started on port: ${port}`);
});
function createUser(req, res) {
const username = slug(req.body.username).toLowerCase();
let newUserData = {
username: username,
password: req.body.password,
name: req.body.name,
birthday: req.body.birthday,
likes: req.body.genre,
email: req.body.email,
favourite: req.body.game,
is: req.body.type,
};
getUserData(dbName).then(async (data) => {
const emailCheck = await data.findOne({ email: req.body.email });
if (emailCheck === null) {
data.insertOne(newUserData);
res.redirect("/profile/" + username);
} else {
res.redirect("/error/" + "email");
}
});
}
function checkForUser(req, res) {
const username = slug(req.body.username).toLowerCase();
getUserData(dbName)
.then((data) =>
data.findOne({
username: req.body.username,
password: req.body.password,
})
)
.then((user) =>
user
? res.redirect("/profile/" + username)
: res.redirect("/error/" + "user")
);
}