qia-server/src/index.ts
ddshi 2c258e4a0c fix: 子模块配置调整
- 移除 Prisma 冗余配置
- 优化 tsconfig.json 支持 ESM
- 调整路由和 API 逻辑
2026-02-02 15:27:06 +08:00

70 lines
1.9 KiB
TypeScript

import 'dotenv/config';
import express, { Application, Request, Response, NextFunction } from 'express';
import cors from 'cors';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
// Import routes
import authRoutes from './routes/auth';
import eventRoutes from './routes/events';
import noteRoutes from './routes/notes';
import aiRoutes from './routes/ai';
// Import error handler
import { errorHandler } from './middleware/errorHandler';
const app: Application = express();
const PORT = process.env.PORT || 3000;
// Security middleware
app.use(helmet());
// Rate limiting - increased for testing
// TEMPORARILY DISABLED FOR TESTING
// const limiter = rateLimit({
// windowMs: 15 * 60 * 1000, // 15 minutes
// max: 500, // limit each IP to 500 requests per windowMs
// message: { error: 'Too many requests, please try again later' },
// standardHeaders: true,
// legacyHeaders: false,
// });
// app.use('/api', limiter);
// CORS configuration - support multiple origins (comma-separated)
const corsOrigins = process.env.CORS_ORIGIN?.split(',').map(o => o.trim()) || ['http://localhost:5173'];
app.use(cors({
origin: corsOrigins.length > 1 ? corsOrigins : corsOrigins[0],
credentials: true,
}));
// Body parser with size limit (prevent DoS attacks)
app.use(express.json({ limit: '10kb' }));
// Health check
app.get('/health', (_req: Request, res: Response) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// API Routes
app.use('/api/auth', authRoutes);
app.use('/api/events', eventRoutes);
app.use('/api/notes', noteRoutes);
app.use('/api/ai', aiRoutes);
// 404 handler
app.use((_req: Request, res: Response) => {
res.status(404).json({ error: 'Not found' });
});
// Error handler
app.use(errorHandler);
// Start server
app.listen(PORT, () => {
console.log(`🚀 Server running on http://localhost:${PORT}`);
console.log(`📝 Environment: ${process.env.NODE_ENV || 'development'}`);
});
export default app;