-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-server.js
62 lines (50 loc) · 1.63 KB
/
express-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
/* eslint-disable no-console */
const express = require('express');
const rateLimit = require('express-rate-limit');
const bodyParser = require('body-parser');
const cors = require('cors');
const escapeHtml = require('escape-html');
const { read, write } = require('./read-write');
const data = [];
const dataFields = new Set(['price', 'size', 'brand', 'color']);
const expressServer = async function expressServer() {
try {
const fileContent = await read('./data.json');
data.push(...JSON.parse(fileContent));
} catch (e) {
console.error(e);
}
const app = express();
app.use(rateLimit({
windowMs: 15 * 60 * 1000,
max: 300,
}));
app.use(cors());
app.use(bodyParser.json());
app.use(express.static('./build'));
app.use(express.static('./dist'));
app.use(express.static('./public'));
app.post('/', async ({ body }, res) => {
console.log(body);
const keys = Object.keys(body).filter((key) => dataFields.has(key));
if (keys.length > 0) {
const sanitizedBody = keys.reduce((acc, e) => ({ ...acc, [e]: escapeHtml(body[e]) }), {});
data.push({ ...sanitizedBody, id: data.length });
try {
await write('./data.json', JSON.stringify(data));
res.status(200).send();
} catch (e) {
console.error(e);
res.status(500).send('dying over here');
}
} else {
res.status(200).send();
}
});
app.get('/data', (_, res) => {
res.send(JSON.stringify(data.filter((e) => e)));
});
const port = process.env.PORT || 8080;
return app.listen(port, () => console.log(`now started listening on port: ${port}`));
};
module.exports = expressServer;