forked from spinsage/express-yt-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (79 loc) · 2.39 KB
/
server.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
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
const express = require('express');
const fs = require('fs');
const port = 8081;
const productsJson = fs.readFileSync(__dirname + '/data/products.json');
const products = JSON.parse(productsJson);
app = express();
app.use(express.json());
app.get('/products', (req, res) => {
res.json(products.productList);
});
app.get('/products/:productId', (req, res) => {
const filteredProducts = products.productList.filter((product) => {
if (product.id == req.params.productId) {
return product;
}
});
if (filteredProducts.length > 0) {
res.json(filteredProducts[0]);
} else {
res.status(404)
res.json(JSON.parse('{}'));
}
});
app.post('/products', (req, res) => {
let newProduct = req.body;
let maxId = 0;
for (let i =0 ; i < products.productList.length; i++) {
if (products.productList[i].id > maxId) {
maxId = products.productList[i].id;
}
}
newProduct.id = maxId + 1;
products.productList.push(newProduct);
res.status(201);
res.json(newProduct);
});
app.put('/products/:productId', (req, res) => {
let updatedProduct = req.body;
let productFound = false;
for (let i = 0; i < products.productList.length; i++) {
console.log(products.productList[i].id);
if (products.productList[i].id == req.params.productId) {
updatedProduct.id = products.productList[i].id;
productFound = true;
products.productList[i] = updatedProduct;
}
}
if (productFound) {
res.json(updatedProduct);
} else {
res.status(404).json(JSON.parse('{}'));
}
});
app.delete('/products/:productId', (req, res) => {
let deletedProduct;
for (let i = 0; i < products.productList.length; i++) {
if (products.productList[i].id == req.params.productId) {
deletedProduct = products.productList[i];
products.productList.splice(i, 1);
break;
}
}
if (deletedProduct) {
res.json(deletedProduct);
} else {
res.status(404);
res.json(JSON.parse('{}'));
}
});
app.all('*', (req, res) => {
const notFoundResponse = {
errCode : 404,
errMessage : 'The operation could not be fulfilles'
}
res.status(404).json(notFoundResponse);
});
app.listen(port, () => {
console.log(`Spinsage Server listening on port ${port}`);
});