-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (85 loc) · 2.37 KB
/
index.js
File metadata and controls
99 lines (85 loc) · 2.37 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
const express = require('express');
const app = express();
var methodOverride = require('method-override');
const path = require('path');
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views')); // Correct path joining
// Serving static files
app.use(express.static(path.join(__dirname, 'public'))); // Correct path joining
const port = 8080;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
let posts = [
{
id: '1',
username: 'Prabal',
content: "Hello peeps what's up?"
},
{
id: '2',
username: 'Kavya',
content: 'kya chal rha hai ?'
},
{
id: '3',
username: 'Prajjwal',
content: 'Behenchod sutta fukne mein itne maze kyu aate hai ?'
}
];
app.get('/posts', (req, res) => {
res.render('index.ejs', { posts });
});
app.get('/posts/new', (req, res) => {
res.render('new.ejs');
});
app.post('/posts', (req, res) => {
let { username, content } = req.body;
let newPost = {
id: `${posts.length + 1}`, // Generate a new ID based on array length
username: username,
content: content
};
posts.push(newPost);
res.redirect('/posts');
});
app.get('/posts/:id', (req, res) => {
let id = req.params.id;
let post = posts.find(p => p.id === id);
if (post) {
res.render('show.ejs', { post });
} else {
res.status(404).send('Post not found');
}
});
app.get('/posts/:id/edit', (req, res) => {
let id = req.params.id;
let post = posts.find(p => p.id === id);
if (post) {
res.render('edit.ejs', { post });
} else {
res.status(404).send('Post not found');
}
});
app.patch('/posts/:id', (req, res) => {
let id = req.params.id;
let newContent = req.body.content;
// Find the post by ID
let postIndex = posts.findIndex(p => p.id === id);
if (postIndex !== -1) {
// Update the content of the found post
posts[postIndex].content = newContent;
res.redirect('/posts');
} else {
// If post is not found, return a 404 error
console.log(`Post with ID ${id} not found.`);
res.status(404).send('Post not found');
}
});
app.delete("/posts/:id", (req, res) => {
let id = req.params.id;
posts = posts.filter(p => p.id !== id);
res.redirect('/posts');
});