-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (50 loc) · 1.82 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
/* eslint-disable no-param-reassign */
const express = require("express");
const bodyParser = require("body-parser");
const fs = require('fs');
const path = require('path');
const app = express();
const LISTINGS_DATA_FILE = path.join(__dirname, 'server-listings-data.json');
const LISTINGS_ORIGINAL_DATA_FILE = path.join(__dirname, 'server-listings-original.json');
app.set("port", process.env.PORT || 3000);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/listings", (_req, res) => {
setTimeout(() => {
fs.readFile(LISTINGS_DATA_FILE, (err, data) => {
res.setHeader('Cache-Control', 'no-cache');
res.json(JSON.parse(data));
});
}, 1000);
});
app.post("/listings/delete", (req, res) => {
fs.readFile(LISTINGS_DATA_FILE, (err, data) => {
const id = req.body.id;
let listingProducts = JSON.parse(data);
for (let i = 0; i < listingProducts.length; i++) {
if (listingProducts[i].id === id) {
listingProducts.splice(i, 1)[0];
}
}
fs.writeFile(LISTINGS_DATA_FILE, JSON.stringify(listingProducts, null, 3), () => {
res.setHeader('Cache-Control', 'no-cache');
res.json(listingProducts);
});
});
});
app.post("/listings/reset", (req, res) => {
fs.readFile(LISTINGS_ORIGINAL_DATA_FILE, (err, data) => {
let listingProducts = JSON.parse(data);
fs.writeFile(LISTINGS_DATA_FILE, JSON.stringify(listingProducts, null, 3), () => {
res.setHeader('Cache-Control', 'no-cache');
res.json(listingProducts);
});
});
});
app.listen(app.get("port"), () => {
console.log( // eslint-disable-line no-console
`Find the server at: http://localhost:${app.get(
"port"
)}/`
);
});