-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
101 lines (75 loc) · 1.74 KB
/
app.js
File metadata and controls
101 lines (75 loc) · 1.74 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require('express')
const { v4: uuid } = require('uuid')
const PORT = 8080
const app = express()
const data = []
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/list', (request, response) => {
// const { body, query, params, url } = request
// const { cod } = params
// const { name, age } = query
// console.log('query: ', url)
// response.write('Node.js v14')
// const json = {
// status: cod,
// auth: true,
// response: {
// name,
// age: Number(age)
// }
// }
response.status(200).json(data)
})
app.post('/list', (request, response) => {
const { body } = request
const json = {
status: 200,
auth: true,
response: {
id: uuid(),
...body
}
}
data.push(json.response)
response.status(200).json(json)
})
app.put('/list/:id', (request, response) => {
const { params, body } = request
const { id } = params
const dataUpdated = data.map((item) => {
if (item.id === id) {
return {
id,
...body
}
}
return item
})
data.splice(0, data.length)
data.push(...dataUpdated)
const json = {
status: 200,
auth: true,
response: dataUpdated.find((item) => item.id === id)
}
response.status(200).json(json)
})
app.delete('/list/:id', (req, res) => {
const { params } = req
const dataUpdated = data.filter((item) => {
if (item.id === params.id) {
return false
}
return item
})
data.splice(0, data.length)
data.push(...dataUpdated)
res.status(200).json({
status: 200,
message: 'Se eliminó el registro con el código ' + params.id
})
})
app.listen(PORT, () => {
console.log(`El servidor está escuchando en el puerto ${PORT}`)
})