server.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // server.js
  2. const express = require('express');
  3. const fs = require('fs').promises;
  4. const path = require('path');
  5. const bodyParser = require('body-parser');
  6. const session = require('express-session');
  7. const app = express();
  8. app.use(express.json());
  9. app.use(express.static('web'));
  10. app.use(bodyParser.urlencoded({ extended: true }));
  11. app.use(session({
  12. secret: 'OhTq3faqSKoxbV%NJV',
  13. resave: false,
  14. saveUninitialized: true,
  15. cookie: { secure: false } // 设置为true如果使用HTTPS
  16. }));
  17. app.get('/admin', (req, res) => {
  18. res.sendFile(path.join(__dirname, 'web', 'admin.html'));
  19. });
  20. const CONFIG_FILE = path.join(__dirname, 'config.json');
  21. const USERS_FILE = path.join(__dirname, 'users.json');
  22. // 读取配置
  23. async function readConfig() {
  24. try {
  25. const data = await fs.readFile(CONFIG_FILE, 'utf8');
  26. return JSON.parse(data);
  27. } catch (error) {
  28. if (error.code === 'ENOENT') {
  29. return {
  30. logo: '',
  31. menuItems: [],
  32. adImage: { url: '', link: '' }
  33. };
  34. }
  35. throw error;
  36. }
  37. }
  38. // 写入配置
  39. async function writeConfig(config) {
  40. await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf8');
  41. }
  42. // 读取用户
  43. async function readUsers() {
  44. try {
  45. const data = await fs.readFile(USERS_FILE, 'utf8');
  46. return JSON.parse(data);
  47. } catch (error) {
  48. if (error.code === 'ENOENT') {
  49. return {
  50. users: [{ username: 'root', password: 'admin' }]
  51. };
  52. }
  53. throw error;
  54. }
  55. }
  56. // 写入用户
  57. async function writeUsers(users) {
  58. await fs.writeFile(USERS_FILE, JSON.stringify(users, null, 2), 'utf8');
  59. }
  60. // 登录验证
  61. app.post('/api/login', async (req, res) => {
  62. const { username, password } = req.body;
  63. const users = await readUsers();
  64. const user = users.users.find(u => u.username === username && u.password === password);
  65. if (user) {
  66. req.session.user = user;
  67. res.json({ success: true });
  68. } else {
  69. res.status(401).json({ error: 'Invalid credentials' });
  70. }
  71. });
  72. // 修改密码
  73. app.post('/api/change-password', async (req, res) => {
  74. if (!req.session.user) {
  75. return res.status(401).json({ error: 'Not logged in' });
  76. }
  77. const { currentPassword, newPassword } = req.body;
  78. const users = await readUsers();
  79. const user = users.users.find(u => u.username === req.session.user.username);
  80. if (user && user.password === currentPassword) {
  81. user.password = newPassword;
  82. await writeUsers(users);
  83. res.json({ success: true });
  84. } else {
  85. res.status(401).json({ error: 'Invalid current password' });
  86. }
  87. });
  88. // 需要登录验证的中间件
  89. function requireLogin(req, res, next) {
  90. if (req.session.user) {
  91. next();
  92. } else {
  93. res.status(401).json({ error: 'Not logged in' });
  94. }
  95. }
  96. // API 端点:获取配置
  97. app.get('/api/config', requireLogin, async (req, res) => {
  98. try {
  99. const config = await readConfig();
  100. res.json(config);
  101. } catch (error) {
  102. res.status(500).json({ error: 'Failed to read config' });
  103. }
  104. });
  105. // API 端点:保存配置
  106. app.post('/api/config', requireLogin, async (req, res) => {
  107. try {
  108. await writeConfig(req.body);
  109. res.json({ success: true });
  110. } catch (error) {
  111. res.status(500).json({ error: 'Failed to save config' });
  112. }
  113. });
  114. // API 端点:检查会话状态
  115. app.get('/api/check-session', (req, res) => {
  116. if (req.session.user) {
  117. res.json({ success: true });
  118. } else {
  119. res.status(401).json({ error: 'Not logged in' });
  120. }
  121. });
  122. // 启动服务器
  123. const PORT = process.env.PORT || 3000;
  124. app.listen(PORT, () => {
  125. console.log(`Server is running on http://localhost:${PORT}`);
  126. });