40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
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}`);
|
|
}); |