-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (71 loc) · 1.9 KB
/
Copy pathindex.js
File metadata and controls
85 lines (71 loc) · 1.9 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
import express from "express";
import ejs from "ejs";
import bodyParser from "body-parser";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const port = 3000;
const app = express();
var posts = [];
var id = -1;
function findPost(id){
const idEdit = parseInt(id);
const post = posts.find((p) => p.postId === idEdit);
return post;
}
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.render("index.ejs", {
userPosts: posts,
hasPosts: posts.length > 0
});
});
app.get("/write", (req, res) => {
res.render("write.ejs");
});
// req.params.id will return whatever the id was in the action
app.get("/edit/:id", (req, res) => {
const post = findPost(req.params["id"]);
if(post) {
res.render("write.ejs", { postEdit: post});
} else {
res.send(`<h1>Cannot find the post with the id ${req.params["id"]}</h1>`);
}
});
app.post("/:id", (req, res) => {
const post = findPost(req.params["id"]);
post["text"] = req.body["post"];
res.render("index.ejs", {
userPosts: posts,
});
});
app.post("/delete/:id", (req, res) => {
const post = findPost(req.params["id"]);
const index = posts.indexOf(post);
if(index > -1) {
posts.splice(index, 1);
id = -1;
posts.forEach((post) => {
post["postId"] = ++id;
});
}
res.render("index.ejs", {
userPosts: posts,
});
})
app.post("/", (req, res) => {
const userPost = {
postId: ++id,
firstName: req.body["fName"],
lastName: req.body["lName"],
text: req.body["post"],
};
posts.push(userPost);
res.render("index.ejs", {
userPosts: posts,
});
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});