-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (48 loc) · 1.36 KB
/
app.js
File metadata and controls
58 lines (48 loc) · 1.36 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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// Set up body-parser
app.use(bodyParser.urlencoded({ extended: true }));
// Set EJS as the view engine
app.set('view engine', 'ejs');
// Serve static files from /public
app.use(express.static('public'));
// Temporary storage for posts
let posts = [];
// Home route - renders posts on the homepage
app.get('/', (req, res) => {
res.render('home', { posts: posts });
});
// Route to handle new post creation
app.post('/new', (req, res) => {
const post = {
id: Date.now().toString(),
creator: req.body.creator,
title: req.body.title,
content: req.body.content,
createdAt: new Date()
};
posts.push(post);
res.redirect('/');
});
// Edit route
app.get('/edit/:id', (req, res) => {
const post = posts.find(p => p.id === req.params.id);
res.render('edit', { post: post });
});
// Update post
app.post('/edit/:id', (req, res) => {
const post = posts.find(p => p.id === req.params.id);
post.title = req.body.title;
post.content = req.body.content;
res.redirect('/');
});
// Delete post
app.post('/delete/:id', (req, res) => {
posts = posts.filter(p => p.id !== req.params.id);
res.redirect('/');
});
// Start server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});