forked from fac-23/app-week1-team1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
125 lines (89 loc) · 3.42 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
114
115
116
117
118
119
120
121
122
123
124
125
const express = require("express");
const server = express();
// MODULES
const posts = require("./posts");
const staticHandler = express.static("public");
server.use(staticHandler);
// home page
server.get("/", (request, response) => {
let allPosts = "";
for (const post of Object.values(posts)) {
allPosts += `<li id =${post.user.replace(/>/g, "<")} class="post">
<div class="post__content">
<h2 class="post__user">${post.user.replace(/>/g, "<")}</h2>
<p class="post__message">${post.message.replace(/>/g, "<")}</p>
</div>
<form action="/deletepost" method="POST" id="delete-post-form">
<button name="name" value="${post.user}" id="delete-post-${post.user}" type="submit" aria-label="Click this button to delete this post">Delete</button>
</form>
</li>`;
}
let html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style.css">
<link rel="shortcut icon" type="image/jpg" href="favicon.ico"/>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Mochiy+Pop+P+One&family=Ubuntu&display=swap" rel="stylesheet">
<title>Tiny Thoughts!</title>
</head>
<body>
<div class="app-wrapper center width-md">
<img src=https://freesvg.org/img/internet_cloud.png>
<h1>Tiny Thoughts!</h1>
<form method="POST" id="submit-post-form" class="width-sm">
<label for="user">User</label>
<input name="user" type="text" id="user">
<label for="message">Message</label>
<textarea name="message" type="text" id="message" rows="5" cols="33"></textarea>
<button aria-label="Click this button submit your post" type="submit">Post</button>
</form>
<ul class="stack-md">${allPosts}</ul>
</div>
</body>
</html>
`;
response.send(html);
});
// ADDING A POST
const bodyParser = express.urlencoded({ extended: false });
server.post("/", bodyParser, (request, response) => {
const newName = request.body.user.replace(/>/g, "<");
const newMessage = request.body.message.replace(/>/g, "<");
const newPost = { user: newName, message: newMessage };
posts[newName.toLowerCase().replace(/>/g, "<")] = newPost;
response.redirect("/");
// console.log(posts);
// console.log(request.body.user);
});
// DELETE POST
server.post("/deletepost", bodyParser, (request, response) => {
const postToDelete = request.body.name.toLowerCase();
console.log(request.body);
delete posts[postToDelete];
response.redirect("/");
});
const PORT = process.env.PORT || 3333;
server.listen(PORT, () => console.log(`http://localhost:${PORT}`));
// redirect to page is not found if 404 status
server.use((request, response) => {
const page404 = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel ='stylesheet' type='text/css' href='status404.css'>
<title>cloud not found</title>
</head>
<body>
<h1>The page your looking for doesn't exist</h1>
</body>
</html>
`
response.status(404).send(page404)
})