-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (29 loc) · 890 Bytes
/
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
import express from 'express';
import createHomepageTemplate from './views/index.js';
import getBookList from './views/book-list.js'
import BOOKS_DATA from './data/data.js';
import bookRow from './views/book-row.js';
const app = express();
app.use(express.urlencoded({extended: false}));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send(createHomepageTemplate());
});
app.get("/books", (req, res) => {
res.send(getBookList())
})
app.post("/books", (req, res) => {
const {title, author} = req.body;
const id = Math.random().toString();
const book = {id,title,author}
BOOKS_DATA.push(book)
res.send(bookRow(book))
})
app.delete('/books/:id', (req, res) => {
const idx = BOOKS_DATA.findIndex(b => b.id === req.params.id);
BOOKS_DATA.splice(idx, 1);
res.send();
});
app.listen(3000, () => {
console.log('App listening on port 3000');
});