Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (94 loc) · 2.83 KB
/
Copy pathindex.js
File metadata and controls
122 lines (94 loc) · 2.83 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const fs = require('fs')
const express = require('express')
const Container = require("./container.js")
const { Router } = express
const app = express()
const router = Router()
//Instance of class Products
const products = new Container('products')
router.use(express.json())
router.use(express.urlencoded({extended:true}))
app.use(express.static('public'))
const PORT = process.env.PORT || 8080
router.get("/new", (req, res) => {
res.sendFile(__dirname + '/public/index.html')
})
//-------Get Products and save them on products array-------->
const bufferReader = async () => {
const data = await fs.promises.readFile('products.json','utf-8')
const productsArray = JSON.parse(data)
productsArray.forEach(product => products.save(product))
}
bufferReader()
//--------API REST---------
router.get('/', (req, res) => {
res.send(products.getAll())
})
//GET ONE
router.get('/:id', (req, res) => {
const id = req.params.id
const product = products.getById(id)
const error = new Error('Product not found')
product ? res.json(product)
: res.json({ error: error.message })
})
//POST for POSTMAN
router.post('/', auth, (req, res) => {
const product = req.body
products.save(product)
res.send(product)
})
//POST form
router.post('/save', auth, (req, res) => {
const { name, price, thumbnail } = req.body
const product = {
name,
price:Number(price),
thumbnail
}
products.save(product)
res.json(product)
})
//PUT
router.put('/:id', (req, res) => {
const id = req.params.id
const product = req.body
if(!products.getById(id)){
const error = new Error('Product not found')
res.json({ error: error.message })
return
}
if(!product){
const error = new Error('Product not found')
return res.status(404).json({ error: error.message })
}
const updatedProduct = products.updateOne(id, product)
res.json(updatedProduct)
})
//DELETE
router.delete('/:id', (req, res) => {
const id = req.params.id
if(!products.getById(id)){
const error = new Error('Product not found')
return res.status(404).json({ error: error.message })
}
products.deleteOne(id)
res.json(products.getAll())
})
// ---------------------Middleware --------------------
function auth(req, res, next) {
if(req.body.name !== "" && req.body.thumbnail !== "" && req.body.price !== "") {
next() //acceso a la ruta y a listar
} else{
const error= new Error(`Error : Missing data, please complete all fields`)
error.httpStatusCode = 404
return next(error)
}
}
app.use('/api/products', router)
const server = app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})
server.on('error', (err) => {
console.log(err)
})