商城国际化JSON编辑器,支持中日对照
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const express = require('express');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const app = express();
  5. // 增加请求体大小限制到 50MB
  6. app.use(express.json({limit: '50mb'}));
  7. app.use(express.urlencoded({limit: '50mb', extended: true}));
  8. // 提供静态文件服务
  9. app.use(express.static(path.join(__dirname, 'public')));
  10. // 读取 ja.js 文件
  11. app.get('/api/data', (req, res) => {
  12. try {
  13. const jaPath = path.join(__dirname, 'ja.js');
  14. const content = fs.readFileSync(jaPath, 'utf8');
  15. // 移除可能的 module.exports 或 export default
  16. const jsonContent = content.replace(/module\.exports\s*=\s*|export\s+default\s+/, '');
  17. const data = eval('(' + jsonContent + ')');
  18. res.json(data);
  19. } catch (error) {
  20. res.status(500).json({ error: error.message });
  21. }
  22. });
  23. // 添加读取 zh.js 的接口
  24. app.get('/api/reference', (req, res) => {
  25. try {
  26. const zhPath = path.join(__dirname, 'zh.js');
  27. const content = fs.readFileSync(zhPath, 'utf8');
  28. // 移除可能的 module.exports 或 export default
  29. const jsonContent = content.replace(/module\.exports\s*=\s*|export\s+default\s+/, '');
  30. const data = eval('(' + jsonContent + ')');
  31. res.json(data);
  32. } catch (error) {
  33. res.status(500).json({ error: error.message });
  34. }
  35. });
  36. // 保存更新后的内容
  37. app.post('/api/save', (req, res) => {
  38. try {
  39. const jaPath = path.join(__dirname, 'ja.js');
  40. const originalContent = fs.readFileSync(jaPath, 'utf8');
  41. const newData = req.body;
  42. // 保持原有的导出格式
  43. let exportFormat = 'module.exports = ';
  44. if (originalContent.includes('export default')) {
  45. exportFormat = 'export default ';
  46. }
  47. // 格式化 JSON,保持原有结构,只更新 value
  48. const content = exportFormat + JSON.stringify(newData, null, 2);
  49. fs.writeFileSync(jaPath, content);
  50. res.json({ success: true });
  51. } catch (error) {
  52. console.error('Save error:', error);
  53. res.status(500).json({
  54. success: false,
  55. error: error.message
  56. });
  57. }
  58. });
  59. // 启动服务器
  60. const port = 3112;
  61. app.listen(port, () => {
  62. console.log(`Server running at http://localhost:${port}`);
  63. });