-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (36 loc) · 1.11 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
const express = require('express')
const path = require('path')
const {v4} = require('uuid')
const app = express()
let CONTACTS = [
{id: v4(), name: 'Татьяна Энтони', value: '+7-921-100-20', marked: false}
]
app.use(express.json())
// GET
app.get('/api/contacts', (req, res) => {
setTimeout(() => {
res.status(200).json(CONTACTS)
}, 1000)
})
// POST
app.post('/api/contacts', (req, res) => {
const contact = {...req.body, id: v4(), marked: false}
CONTACTS.push(contact)
res.status(201).json(contact)
})
// DELETE
app.delete('/api/contacts/:id', (req, res) => {
CONTACTS = CONTACTS.filter(c => c.id !== req.params.id)
res.status(200).json({message: 'Контакт был удален'})
})
// PUT
app.put('/api/contacts/:id', (req, res) => {
const idx = CONTACTS.findIndex(c => c.id === req.params.id)
CONTACTS[idx] = req.body
res.json(CONTACTS[idx])
})
app.use(express.static(path.resolve(__dirname, 'client')))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'index.html'))
})
app.listen(3000, () => console.log('Server has been started on port 3000...'))