1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- const express = require('express');
- const fs = require('fs');
- const path = require('path');
- const app = express();
-
- // 增加请求体大小限制到 50MB
- app.use(express.json({limit: '50mb'}));
- app.use(express.urlencoded({limit: '50mb', extended: true}));
-
- // 提供静态文件服务
- app.use(express.static(path.join(__dirname, 'public')));
-
- // 读取 ja.js 文件
- app.get('/api/data', (req, res) => {
- try {
- const jaPath = path.join(__dirname, 'ja.js');
- const content = fs.readFileSync(jaPath, 'utf8');
- // 移除可能的 module.exports 或 export default
- const jsonContent = content.replace(/module\.exports\s*=\s*|export\s+default\s+/, '');
- const data = eval('(' + jsonContent + ')');
- res.json(data);
- } catch (error) {
- res.status(500).json({ error: error.message });
- }
- });
-
- // 添加读取 zh.js 的接口
- app.get('/api/reference', (req, res) => {
- try {
- const zhPath = path.join(__dirname, 'zh.js');
- const content = fs.readFileSync(zhPath, 'utf8');
- // 移除可能的 module.exports 或 export default
- const jsonContent = content.replace(/module\.exports\s*=\s*|export\s+default\s+/, '');
- const data = eval('(' + jsonContent + ')');
- res.json(data);
- } catch (error) {
- res.status(500).json({ error: error.message });
- }
- });
-
- // 保存更新后的内容
- app.post('/api/save', (req, res) => {
- try {
- const jaPath = path.join(__dirname, 'ja.js');
- const originalContent = fs.readFileSync(jaPath, 'utf8');
- const newData = req.body;
-
- // 保持原有的导出格式
- let exportFormat = 'module.exports = ';
- if (originalContent.includes('export default')) {
- exportFormat = 'export default ';
- }
-
- // 格式化 JSON,保持原有结构,只更新 value
- const content = exportFormat + JSON.stringify(newData, null, 2);
- fs.writeFileSync(jaPath, content);
- res.json({ success: true });
- } catch (error) {
- console.error('Save error:', error);
- res.status(500).json({
- success: false,
- error: error.message
- });
- }
- });
-
- // 启动服务器
- const port = 3112;
- app.listen(port, () => {
- console.log(`Server running at http://localhost:${port}`);
- });
|