feat: Initial commit of PDF Tools project
This commit is contained in:
82
server/index.js
Normal file
82
server/index.js
Normal file
@@ -0,0 +1,82 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const helmet = require('helmet');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const path = require('path');
|
||||
const dotenv = require('dotenv');
|
||||
|
||||
// 导入路由
|
||||
const fileRoutes = require('./routes/files');
|
||||
const conversionRoutes = require('./routes/conversion');
|
||||
const userRoutes = require('./routes/users');
|
||||
const systemRoutes = require('./routes/system');
|
||||
|
||||
// 导入中间件
|
||||
const errorHandler = require('./middleware/errorHandler');
|
||||
const authMiddleware = require('./middleware/auth');
|
||||
|
||||
// 加载环境变量
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
|
||||
// 安全中间件
|
||||
app.use(helmet());
|
||||
|
||||
// CORS配置
|
||||
app.use(cors({
|
||||
origin: process.env.CLIENT_URL || 'http://localhost:3000',
|
||||
credentials: true
|
||||
}));
|
||||
|
||||
// 请求限流
|
||||
const limiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15分钟
|
||||
max: 100, // 每个IP最多100个请求
|
||||
message: '请求过于频繁,请稍后再试'
|
||||
});
|
||||
app.use('/api/', limiter);
|
||||
|
||||
// 解析请求体
|
||||
app.use(express.json({ limit: '50mb' }));
|
||||
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
|
||||
|
||||
// 静态文件服务
|
||||
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
|
||||
|
||||
// API路由
|
||||
app.use('/api/files', fileRoutes);
|
||||
app.use('/api/convert', conversionRoutes);
|
||||
app.use('/api/users', userRoutes);
|
||||
app.use('/api/system', systemRoutes);
|
||||
|
||||
// 健康检查端点
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'OK',
|
||||
timestamp: new Date().toISOString(),
|
||||
uptime: process.uptime(),
|
||||
memory: process.memoryUsage()
|
||||
});
|
||||
});
|
||||
|
||||
// 错误处理中间件
|
||||
app.use(errorHandler);
|
||||
|
||||
// 404处理
|
||||
app.use('*', (req, res) => {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
message: '接口不存在',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 PDF转换工具服务器运行在端口 ${PORT}`);
|
||||
console.log(`📊 健康检查: http://localhost:${PORT}/health`);
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
Reference in New Issue
Block a user