Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (51 loc) · 1.49 KB
/
Copy pathserver.js
File metadata and controls
63 lines (51 loc) · 1.49 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
const express = require("express");
const bodyParser = require("body-parser"); // Import body-parser
const app = express();
const port = process.env.PORT || 9200;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true })); // Enable body-parser
const log = [];
const teams = [];
// Home route
app.get("/", (req, res) => {
// Return html file
return res.sendFile(__dirname + "/public/index.html");
});
// Tappers route (dynamic log)
app.get("/tappers", (req, res) => {
// Return html file
return res.sendFile(__dirname + "/public/tappers.html");
});
// Log route (JSON)
app.get("/log", (req, res) => {
// Invert the array to have the latest entries on top
const rlog = log.slice().reverse();
const json = {
log: rlog,
teams: teams,
};
res.json(json);
});
// Tap API route
app.post("/log", (req, res) => {
const userName = req.query.userName || "Anonymous";
const options = { timeZone: "Europe/Rome", hour12: false };
const timestamp = new Date().toLocaleTimeString("it-IT", options);
if (userName === "admin") {
// Clean the log and the teams array
log.length = 0;
teams.length = 0;
} else {
const entry = `${userName} ha premuto alle ${timestamp}`;
// Add the user to the teams array if not already present
if (!teams.includes(userName)) {
teams.push(userName);
}
log.push(entry);
}
res.sendStatus(200);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});