File size: 2,023 Bytes
b7560a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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')));
// API路由
app.use('/api/auth', authRoutes);
app.use('/api/ppt', authenticateToken, pptRoutes);
app.use('/api/public', publicRoutes);
// 健康检查
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// 前端路由处理 - 必须在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'}`);
}); |