-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
31 lines (24 loc) · 802 Bytes
/
Copy pathserver.js
File metadata and controls
31 lines (24 loc) · 802 Bytes
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
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
app.use(express.static(__dirname));
app.use(express.json());
// Support both routes for development and production
app.post(['/save-time', '/api/save-time'], (req, res) => {
const filePath = path.join(__dirname, 'time.json');
let times = [];
// Read existing times
if (fs.existsSync(filePath)) {
times = JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
// Add new time
times.push(req.body);
// Save back to file
fs.writeFileSync(filePath, JSON.stringify(times, null, 2));
res.json({ success: true });
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});