-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
55 lines (44 loc) · 1.25 KB
/
app.js
File metadata and controls
55 lines (44 loc) · 1.25 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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
const blogs = [];
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index', { blogs: blogs });
});
app.post('/create', (req, res) => {
const blog = {
title: req.body.title,
author: req.body.author,
content: req.body.content,
date: new Date()
};
blogs.push(blog);
res.redirect('/');
});
app.get('/edit/:id', (req, res) => {
const blogId = req.params.id;
const blog = blogs[blogId];
res.render('edit', { blog: blog, id: blogId });
});
app.post('/edit/:id', (req, res) => {
const blogId = req.params.id;
blogs[blogId] = {
title: req.body.title,
author: req.body.author,
content: req.body.content,
date: blogs[blogId].date
};
res.redirect('/');
});
app.get('/delete/:id', (req, res) => {
const blogId = req.params.id;
blogs.splice(blogId, 1);
res.redirect('/');
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});