-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
126 lines (103 loc) · 3.01 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const express = require("express");
const EventEmitter = require("events");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
const Stream = new EventEmitter();
app.use(bodyParser.json());
// We don't use a real database, because we don't care if the server crashes,
// and we don't want to store any information more than 1 pub.
let currentFood = []; // string[]
app.get("/subscribe", (req, res) => {
res.writeHead(200, {
Connection: "keep-alive",
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache"
});
function onPush(event, data) {
res.write(`data: ${data} \n\n`);
}
Stream.on("push", onPush);
req.on("close", function() {
Stream.removeListener("push", onPush);
});
// Give list to new clients
sendList();
});
app.post("/addOrRemove", (req, res) => {
const id = req.body.id.toString(); // we need this to check length
if (id.length < 9 && !isNaN(id)) {
const operation = addOrRemoveFood(id);
return res.json({ message: `food was ${operation}` });
}
res.status(400).json({
message: "Bad Request: id must be 1-8 digit integer."
});
});
app.post("/add", (req, res) => {
const id = req.body.id.toString(); // we need this to check length
if (id.length < 9 && !isNaN(id)) {
const operation = addFood(id);
return res.json({ message: `food was ${operation}` });
}
res.status(400).json({
message: "Bad Request: id must be 1-8 digit integer."
});
});
app.post("/del", (req, res) => {
const id = req.body.id.toString(); // we need this to check length
if (id.length < 9 && !isNaN(id)) {
const operation = delFood(id);
return res.json({ message: `food was ${operation}` });
}
res.status(400).json({
message: "Bad Request: id must be 1-8 digit integer."
});
});
app.post("/clear", (req, res) => {
clearList();
res.json({ message: "foodlist was cleared" });
});
app.use(express.static(path.join(__dirname, "front-end/build")));
app.get("*", (req, res) =>
res.sendFile(path.join(`${__dirname}/front-end/build/index.html`))
); // Handles 404
const port = process.env.PORT || 3001;
app.listen(port, () =>
console.log("\nApp started at http://localhost:" + port)
);
// Helpers that should be in another file
const sendList = () => Stream.emit("push", "list", JSON.stringify(currentFood));
const clearList = () => {
currentFood = [];
sendList();
};
const addOrRemoveFood = num => {
if (!currentFood.includes(num)) {
currentFood = currentFood.concat(num);
sendList();
return "added";
} else {
currentFood = currentFood.filter(item => item !== num);
sendList();
return "removed";
}
};
const addFood = num => {
if (!currentFood.includes(num)) {
currentFood = currentFood.concat(num);
sendList();
return "added";
} else {
return "no change";
}
};
const delFood = num => {
if (currentFood.includes(num)) {
currentFood = currentFood.filter(item => item !== num);
sendList();
return "removed";
} else {
return "no change";
}
};