2025-08-30 14:33:49 +08:00
|
|
|
|
const express = require('express')
|
|
|
|
|
|
const cors = require('cors')
|
|
|
|
|
|
const helmet = require('helmet')
|
|
|
|
|
|
const morgan = require('morgan')
|
|
|
|
|
|
const rateLimit = require('express-rate-limit')
|
|
|
|
|
|
const xss = require('xss-clean')
|
|
|
|
|
|
const hpp = require('hpp')
|
2025-08-30 15:29:51 +08:00
|
|
|
|
const swaggerUi = require('swagger-ui-express')
|
|
|
|
|
|
const swaggerSpec = require('./config/swagger')
|
2025-08-30 14:33:49 +08:00
|
|
|
|
|
|
|
|
|
|
console.log('🔧 初始化Express应用...')
|
|
|
|
|
|
|
|
|
|
|
|
const { globalErrorHandler, notFound } = require('./utils/errors')
|
|
|
|
|
|
|
|
|
|
|
|
// 路由导入
|
|
|
|
|
|
const authRoutes = require('./routes/auth')
|
|
|
|
|
|
// 其他路由将在这里导入
|
|
|
|
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
|
|
|
|
console.log('✅ Express应用初始化完成')
|
|
|
|
|
|
|
|
|
|
|
|
// 安全中间件
|
|
|
|
|
|
app.use(helmet())
|
|
|
|
|
|
|
|
|
|
|
|
// CORS配置
|
|
|
|
|
|
app.use(cors({
|
|
|
|
|
|
origin: process.env.NODE_ENV === 'production'
|
|
|
|
|
|
? ['https://your-domain.com']
|
|
|
|
|
|
: ['http://localhost:9000', 'http://localhost:3000'],
|
|
|
|
|
|
credentials: true
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
// 请求日志
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
|
app.use(morgan('dev'))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
app.use(morgan('combined'))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 请求频率限制
|
|
|
|
|
|
const limiter = rateLimit({
|
|
|
|
|
|
windowMs: 15 * 60 * 1000, // 15分钟
|
|
|
|
|
|
max: process.env.NODE_ENV === 'production' ? 100 : 1000, // 生产环境100次,开发环境1000次
|
|
|
|
|
|
message: {
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
code: 429,
|
|
|
|
|
|
message: '请求过于频繁,请稍后再试',
|
|
|
|
|
|
timestamp: new Date().toISOString()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
app.use('/api', limiter)
|
|
|
|
|
|
|
|
|
|
|
|
// 请求体解析
|
|
|
|
|
|
app.use(express.json({ limit: '10kb' }))
|
|
|
|
|
|
app.use(express.urlencoded({ extended: true, limit: '10kb' }))
|
|
|
|
|
|
|
|
|
|
|
|
// 数据清洗
|
|
|
|
|
|
app.use(xss()) // 防止XSS攻击
|
|
|
|
|
|
app.use(hpp({ // 防止参数污染
|
|
|
|
|
|
whitelist: [
|
|
|
|
|
|
'page',
|
|
|
|
|
|
'pageSize',
|
|
|
|
|
|
'sort',
|
|
|
|
|
|
'fields',
|
|
|
|
|
|
'price',
|
|
|
|
|
|
'rating',
|
|
|
|
|
|
'distance'
|
|
|
|
|
|
]
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
// 静态文件服务
|
|
|
|
|
|
app.use('/uploads', express.static('uploads'))
|
|
|
|
|
|
|
2025-08-30 15:29:51 +08:00
|
|
|
|
// Swagger文档路由
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development' || process.env.ENABLE_SWAGGER === 'true') {
|
|
|
|
|
|
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))
|
|
|
|
|
|
console.log('📚 Swagger文档已启用: http://localhost:3001/api-docs')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 14:33:49 +08:00
|
|
|
|
// 健康检查路由
|
|
|
|
|
|
app.get('/health', (req, res) => {
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
|
status: 'OK',
|
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
|
uptime: process.uptime(),
|
|
|
|
|
|
environment: process.env.NODE_ENV || 'development'
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// API路由
|
|
|
|
|
|
app.use('/api/v1/auth', authRoutes)
|
|
|
|
|
|
// 其他API路由将在这里添加
|
|
|
|
|
|
// app.use('/api/v1/users', userRoutes)
|
|
|
|
|
|
// app.use('/api/v1/travel', travelRoutes)
|
|
|
|
|
|
// app.use('/api/v1/animals', animalRoutes)
|
|
|
|
|
|
// app.use('/api/v1/flowers', flowerRoutes)
|
|
|
|
|
|
// app.use('/api/v1/orders', orderRoutes)
|
|
|
|
|
|
|
|
|
|
|
|
// 404处理
|
|
|
|
|
|
app.use('*', notFound)
|
|
|
|
|
|
|
|
|
|
|
|
// 全局错误处理
|
|
|
|
|
|
app.use(globalErrorHandler)
|
|
|
|
|
|
|
|
|
|
|
|
console.log('✅ 应用配置完成')
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = app
|