-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (55 loc) · 1.99 KB
/
server.js
File metadata and controls
71 lines (55 loc) · 1.99 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
64
65
66
67
68
69
import express from 'express';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import cors from 'cors';
import bodyParser from 'body-parser';
const app = express();
const port = 3002;
app.use(cors());
// Necessary for __dirname in ES6 modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb', extended: true, parameterLimit: 50000}));
app.get('/api/graphs', async (req, res) => {
const graphDirectory = path.join(__dirname, 'public', 'graphs');
try {
const files = await fs.promises.readdir(graphDirectory);
const graphFiles = files.filter(file => file.endsWith('.json'));
res.json(graphFiles.sort().reverse());
} catch (err) {
console.error('Error:', err);
res.status(500).send('Internal Server Error');
}
});
app.put('/scene.json', async (req, res) => {
const sceneFilePath = path.join(__dirname, 'public', 'scene.json');
try {
await fs.promises.writeFile(sceneFilePath, JSON.stringify(req.body, null, 2));
res.sendStatus(200);
} catch (err) {
console.error('Error updating scene data:', err);
res.status(500).send('Internal Server Error');
}
});
app.post('/save-graph', async (req, res) => {
const graphData = req.body;
// Generate a unique filename for the new graph
const timestamp = new Date().toISOString().replace(/[-:.]/g, '');
const filename = `graph_${timestamp}.json`;
try {
// Save the graph data to a new file in the public/graphs directory
await fs.promises.writeFile(`public/graphs/${filename}`, JSON.stringify(graphData, null, 2));
console.log('Graph saved successfully');
res.sendStatus(200);
} catch (err) {
console.error('Error saving graph:', err);
res.sendStatus(500);
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});