add:添加保存scheme为对象的功能

This commit is contained in:
lhj
2024-11-12 00:09:11 +08:00
parent c9c336ec45
commit 9823540dc4
5 changed files with 166 additions and 79 deletions

40
backend/index.js Normal file
View File

@ -0,0 +1,40 @@
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const app = express();
const PORT = 3000;
// 获取当前文件的目录路径
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const FILE_PATH = path.join(__dirname, '../src/modules/previewScheme.json');
app.use(cors());
app.use(bodyParser.json());
app.post('/save', (req, res) => {
const data = req.body;
try {
fs.writeFileSync(FILE_PATH, JSON.stringify(data, null, 2));
res.status(200).send('Data saved successfully');
} catch (error) {
res.status(500).send('Error saving data');
}
});
app.get('/load', (req, res) => {
try {
const data = fs.readFileSync(FILE_PATH, 'utf-8');
res.status(200).json(JSON.parse(data));
} catch (error) {
res.status(500).send('Error loading data');
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

20
backend/package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "backend",
"version": "1.0.0",
"type": "module",
"main": "dist/index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"express": "^4.21.1"
},
"volta": {
"node": "22.11.0"
},
"devDependencies": {
"@types/cors": "^2.8.17"
}
}