-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (28 loc) · 877 Bytes
/
Copy pathserver.js
File metadata and controls
35 lines (28 loc) · 877 Bytes
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
const express = require('express')
const bodyParser = require('body-parser')
const books = require('./db')
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.get('/books', (req, res) => {
res.json(books)
})
app.get('/books/:id', (req, res) => {
res.json(books.find(book => book.id === req.params.id))
})
app.post('/books', (req, res) => {
books.push(req.body)
res.status(201).json(req.body)
})
app.put('/books/:id', (req, res) => {
const updateIndex = books.findIndex(book => book.id === req.params.id)
res.json(Object.assign(books[updateIndex], req.body))
})
app.delete('/books/:id', (req, res) => {
const deleteIndex = books.findIndex(book => book.id === req.params.id)
books.splice(deleteIndex, 1)
res.status(204).send()
})
app.listen(3000, () => {
console.log('Start server at port 3000.')
})