Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (60 loc) · 1.92 KB
/
Copy pathindex.js
File metadata and controls
83 lines (60 loc) · 1.92 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
import express from "express";
import bodyParser from "body-parser";
import path from 'path';
import { fileURLToPath } from 'url';
const app = express();
const port = 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
let posts = [];
app.get("/", (req, res) => {
res.render("index.ejs", { posts: posts });
});
app.get('/posts/edit/:index', (req, res) => {
const id = req.params.index;
const postToEdit = posts[id];
res.render("edit.ejs", { post: postToEdit, index: id });
});
//stats for more page
app.get("/more", (req, res) => {
const totalBlogs = posts.length;
let totalWords = 0;
posts.forEach(post => {
const words = post.content.trim().split(/\s+/);
totalWords += post.content.trim() === "" ? 0 : words.length;
});
res.render("more.ejs", {
totalBlogs: totalBlogs,
totalWords: totalWords
});
});
app.post('/posts/update/:index', (req, res) => {
const id = req.params.index;
// Overwrite the old object at that specific array index with the new form data
posts[id] = {
title: req.body.title,
content: req.body.content
};
console.log(`--- Post Updated at index ${id} ---`);
// Send them back to the homepage to see the updated changes!
res.redirect('/');
});
app.post('/posts', (req, res) => {
const title = req.body.title;
const content = req.body.content;
console.log("--- New Blog Post Submitted ---");
console.log("Title Entered:", title);
console.log("Content Entered:", content);
posts.push({ title: title, content: content });
res.redirect('/');
});
app.post('/posts/delete/:index', (req, res) => {
const indexToDelete = req.params.index;
posts.splice(indexToDelete, 1);
res.redirect('/?message=Post is deleted');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});