-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (47 loc) · 1.29 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const db = require('./database');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
// Routes
app.get('/', (req, res) => {
db.readItems((err, items) => {
if (err) {
res.send("Error encountered while displaying.");
return console.error(err.message);
}
res.render('index', { items: items });
});
});
app.post('/add', (req, res) => {
const { name, description } = req.body;
db.createItem(name, description, (err) => {
if (err) {
return console.error(err.message);
}
res.redirect('/');
});
});
app.post('/update', (req, res) => {
const { id, name, description } = req.body;
db.updateItem(id, name, description, (err) => {
if (err) {
return console.error(err.message);
}
res.redirect('/');
});
});
app.post('/delete', (req, res) => {
const { id } = req.body;
db.deleteItem(id, (err) => {
if (err) {
return console.error(err.message);
}
res.redirect('/');
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});