-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (118 loc) · 3.86 KB
/
index.js
File metadata and controls
137 lines (118 loc) · 3.86 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
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
// server code
const express = require("express");
const app = express();
const port = 8080;
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
// path required code
const path = require("path");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const methodOverride = require("method-override");
const { v4: uuidv4 } = require("uuid");
app.use(methodOverride("_method"));
// now lets create aejs file for home page and the data arry for that
let posts = [
{
username: "Prem",
content:
"I enjoy working with Java because it helps me build strong logic and understand backend development. Writing Java programs improves my problem-solving skills and confidence. I like learning how real-world applications are designed using it.",
id: uuidv4(),
},
{
username: "Abhilash",
content:
"Python feels simple yet powerful, which makes it great for learning and problem solving. I like using it for logical thinking and small projects. Its clean syntax makes coding less stressful.",
id: uuidv4(),
},
{
username: "Ayushman",
content:
"JavaScript allows me to create interactive and dynamic web pages. I enjoy how it connects frontend and backend logic together. Learning JavaScript helps me understand full-stack development.",
id: uuidv4(),
},
{
username: "Mantosh",
content:
"Spring is useful for building scalable backend applications. I like how it manages complex logic in an organized way. Learning Spring helps me understand enterprise-level development.",
id: uuidv4(),
},
{
username: "Abhisake",
content:
"Docker makes application deployment easier and more reliable. I like how it helps in running apps anywhere without issues. Learning Docker improves my DevOps understanding.",
id: uuidv4(),
},
{
username: "Bishal",
content:
"Startups inspire me because they focus on solving real-life problems. I enjoy learning how ideas turn into products. Reading about startups motivates me to think creatively.",
id: uuidv4(),
},
];
app.get("/posts", (req, res) => {
res.render("home.ejs", { posts });
});
app.get("/post/detail-page/:id", (req, res) => {
let { id } = req.params;
let post = posts.find((p) => id === p.id);
res.render("detail.ejs", { post });
});
// show the edit page
app.get("/post/edit/:id", (req, res) => {
let { id } = req.params;
let post = posts.find((p) => p.id === id);
res.render("edit.ejs", { post });
});
// edit the content by the post req by the client
app.patch("/post/edit/:id", (req, res) => {
let { id } = req.params;
let newcontent = req.body.content;
let post = posts.find((p) => p.id === id);
post.content = newcontent;
res.redirect("/posts");
});
// create new post
app.get("/post/new", (req, res) => {
res.render("newpost.ejs");
});
app.post("/post/new", (req, res) => {
let id = uuidv4();
let newC = req.body.content;
let newU = req.body.username;
let newPost = {
id,
username: newU,
content: newC,
};
posts.push(newPost);
res.redirect("/posts");
});
// delete
app.delete("/post/delete/:id", (req, res) => {
let { id } = req.params;
posts = posts.filter((p) => p.id !== id);
res.redirect("/posts");
});
app.get("/post/new/:id", (req, res) => {
let { id } = req.params;
let post = posts.find((p) => p.id === id);
if (!post) return res.send("Post not found");
res.render("newWithId.ejs", { post });
});
app.post("/post/new/:id", (req, res) => {
let id = req.params.id;
let post = posts.find((p) => p.id === id);
if (!post) return res.send("Post not found");
let newPost = {
id: uuidv4(),
username: post.username,
content: req.body.content,
};
posts.push(newPost);
res.redirect("/posts");
});