-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (50 loc) · 1.5 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
const express = require("express");
const app = express();
const path = require("path");
const fs = require("fs");
// Middleware to parse JSON bodies
app.use(express.json());
// Serve static files (CSS, JS, images, etc.) from the "public" directory
app.use(express.static(path.join(__dirname, "public")));
// Route to serve the HTML file
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Route to handle click events
app.post("/click", (req, res) => {
let clickData = req.body;
// Read existing data from JSON file
fs.readFile("click_data.json", "utf8", (err, jsonString) => {
if (err) {
console.log("Error reading file:", err);
res.status(500).send("Error reading file");
} else {
let data = [];
if (jsonString) {
data = JSON.parse(jsonString);
}
// Push the click data object to the array
data.push(clickData);
// Write updated data to JSON file
fs.writeFile(
"click_data.json",
JSON.stringify(data, null, 2),
"utf8",
(err) => {
if (err) {
console.log("Error writing file:", err);
res.status(500).send("Error writing file");
} else {
console.log("Data saved to click_data.json");
res.send("Click data received.");
}
}
);
}
});
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});