-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
210 lines (184 loc) · 5.16 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import express from "express";
import bodyParser from "body-parser";
import ShortUniqueId from "short-unique-id";
import { dirname } from "path";
import { fileURLToPath } from "url";
import { writeFile } from "fs/promises";
import { readFile } from "fs/promises";
const __filename = dirname(fileURLToPath(import.meta.url));
const uid = new ShortUniqueId({ length: 10 });
const app = express();
const port = 3000;
let blogPosts = [];
let loginDetails = false;
const myUserName = "admin";
const myPassword = "1234";
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__filename + "/public"));
// Add password checking middleware $fun.
function passwordCheck(req, res, next) {
let clientPass = req.body["password"];
let clientUserName = req.body["username"];
if (myPassword === clientPass && myUserName === clientUserName) {
loginDetails = true;
}
next();
}
app.use(passwordCheck);
// Save our blog posts to File and object is converted to a string with JSON method.
async function saveBlogPost() {
try {
const promise = writeFile(
"blog-post.json",
JSON.stringify(blogPosts),
"utf8"
);
await promise;
console.log("Post saved successfully");
} catch (err) {
console.error("It's Some ", err.name);
throw err;
}
}
// Read JSON file and convert string in to javascript obj.
async function readBlogPost() {
try {
const contents = await readFile("blog-post.json", { encoding: "utf8" });
if (contents == 0) {
return [];
}
return JSON.parse(contents);
} catch (err) {
console.error("It's Some ", err.name);
return [];
}
}
// Get Clicked Post Details
function getClickedPostData(postData) {
const blogPostData = blogPosts.find(blogDetailsFind);
function blogDetailsFind(details) {
return details.id === postData;
}
console.log("We Got Post Detail");
// console.log(blogPostData);
return blogPostData;
}
// load all blog posts in Home page route.
readBlogPost().then((allPosts) => {
blogPosts = allPosts;
});
// Initialize web application and set up routes.
app.get("/", (req, res) => {
res.render(__filename + "/views/index.ejs", {
postContent: blogPosts,
});
});
// login page route
app.get("/user_login", (req, res) => {
res.render(__filename + "/views/login_form.ejs");
});
// login check
app.post("/login", (req, res) => {
if (loginDetails) {
res.redirect("/");
}
});
// create new post
app.get("/create_new_post", (req, res) => {
res.render(__filename + "/views/create_post.ejs");
});
// Add new blog posts and save.
app.post("/submit", async (req, res) => {
const title = req.body["title"];
const user = req.body["user"];
const date = req.body["date"];
const content = req.body["content"];
const newBlogPost = {
id: uid.rnd(),
title: title,
user: user,
date: date,
content: content,
};
blogPosts.push(newBlogPost);
try {
await saveBlogPost();
res.redirect("/");
} catch (err) {
console.error("It's Some ", err.name);
throw err;
}
});
// Blog post view
app.use("/view_posts/:postsId", async (req, res) => {
try {
const getPostId = req.params.postsId;
const clickedPostData = getClickedPostData(getPostId);
res.render(__filename + "/views/view_post.ejs", {
postsData: clickedPostData,
});
} catch (err) {
console.error("It's Some ", err.name);
throw err;
}
});
// Blog post edit get.
app.use("/edit/:postUId", async (req, res) => {
try {
const getPostUId = req.params.postUId;
const clickedPostData = getClickedPostData(getPostUId);
if (clickedPostData) {
res.render(__filename + "/views/update_post.ejs", {
postData: clickedPostData,
});
}
} catch (err) {
console.error("It's Some", err.name);
throw err;
}
});
// Blog post edit and update.
app.post("/update", async (req, res) => {
try {
const pId = req.body["id"];
const eTitle = req.body["title"];
const eUser = req.body["user"];
const eDate = req.body["date"];
const eContent = req.body["content"];
const updateBlogPost = {
id: pId,
title: eTitle,
user: eUser,
date: eDate,
content: eContent,
};
const findBlogPostIndex = blogPosts.findIndex(
(blogPost) => blogPost.id === pId
);
blogPosts[findBlogPostIndex] = updateBlogPost;
await saveBlogPost();
res.redirect(`/view_posts/${pId}`);
} catch (error) {
console.error("It's Some", error.name);
throw error;
}
});
// Blog post delete by using array filter method.
app.use("/delete/:postId", async (req, res) => {
try {
const blogPostId = req.params.postId;
const filterBlogPost = blogPosts.filter(
(blogPost) => blogPost.id !== blogPostId
);
blogPosts = filterBlogPost;
await saveBlogPost();
console.log("posts saving successful");
res.redirect("/");
} catch (error) {
console.error("It's Some", error.name);
throw error;
}
});
app.listen(port, () => {
console.log(`Server is running on ${port}.`);
});