import express from 'express'; import cors from 'cors'; import helmet from 'helmet'; import rateLimit from 'express-rate-limit'; import dotenv from 'dotenv'; import path from 'path'; import { fileURLToPath } from 'url'; import authRoutes from './routes/auth.js'; import pptRoutes from './routes/ppt.js'; import publicRoutes from './routes/public.js'; import { authenticateToken } from './middleware/auth.js'; import { errorHandler } from './middleware/errorHandler.js'; dotenv.config(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); const PORT = process.env.PORT || 7860; // 修改为7860端口 // 安全中间件 app.use(helmet({ contentSecurityPolicy: false, // 为了兼容前端静态文件 })); // 限流中间件 const limiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15分钟 max: 100, // 每个IP每15分钟最多100个请求 message: 'Too many requests from this IP, please try again later.' }); app.use('/api', limiter); // CORS配置 app.use(cors({ origin: process.env.FRONTEND_URL || '*', credentials: true })); app.use(express.json({ limit: '50mb' })); app.use(express.urlencoded({ extended: true, limit: '50mb' })); // 提供前端静态文件 app.use(express.static(path.join(__dirname, '../../frontend/dist'))); // 提供数据文件 app.use('/data', express.static(path.join(__dirname, '../../frontend/public/mocks'))); // API路由 console.log('Registering API routes...'); // 健康检查 - 需要在其他路由之前 app.get('/api/health', (req, res) => { res.json({ status: 'ok', timestamp: new Date().toISOString() }); }); // GitHub连接状态检查 app.get('/api/github/status', async (req, res) => { try { const { default: githubService } = await import('./services/githubService.js'); const validation = await githubService.validateConnection(); res.json({ github: validation, environment: { tokenConfigured: !!process.env.GITHUB_TOKEN, reposConfigured: !!process.env.GITHUB_REPOS, nodeEnv: process.env.NODE_ENV } }); } catch (error) { res.status(500).json({ error: error.message }); } }); // 添加路由注册日志 console.log('Importing route modules...'); console.log('Auth routes imported:', !!authRoutes); console.log('PPT routes imported:', !!pptRoutes); console.log('Public routes imported:', !!publicRoutes); // 认证相关路由(不需要认证) app.use('/api/auth', authRoutes); // 公共访问路由(不需要认证) app.use('/api/public', publicRoutes); // 添加测试路由来验证 PPT 路由是否工作(不需要认证) app.get('/api/ppt/test', (req, res) => { res.json({ message: 'PPT routes are working', timestamp: new Date().toISOString() }); }); // PPT管理路由(需要认证) app.use('/api/ppt', (req, res, next) => { console.log(`PPT route accessed: ${req.method} ${req.path}`); next(); }, authenticateToken, pptRoutes); console.log('All routes registered successfully'); // 添加调试中间件 - 处理未匹配的API路由 app.use('/api/*', (req, res) => { console.log(`Unmatched API route: ${req.method} ${req.path}`); res.status(404).json({ error: 'API route not found', path: req.path }); }); // 前端路由处理 - 必须在API路由之后 app.get('*', (req, res) => { res.sendFile(path.join(__dirname, '../../frontend/dist/index.html')); }); // 错误处理中间件 app.use(errorHandler); app.listen(PORT, '0.0.0.0', () => { console.log(`Server is running on port ${PORT}`); console.log(`Environment: ${process.env.NODE_ENV || 'development'}`); });