保险前后端,养殖端和保险端小程序
This commit is contained in:
116
insurance_backend/src/app.js
Normal file
116
insurance_backend/src/app.js
Normal file
@@ -0,0 +1,116 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const helmet = require('helmet');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const swaggerUi = require('swagger-ui-express');
|
||||
const swaggerSpec = require('../config/swagger');
|
||||
const { sequelize, testConnection } = require('../config/database');
|
||||
require('dotenv').config();
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// 安全中间件
|
||||
app.use(helmet());
|
||||
app.use(cors({
|
||||
origin: process.env.FRONTEND_URL || 'http://localhost:5173',
|
||||
credentials: true
|
||||
}));
|
||||
|
||||
// 速率限制
|
||||
const limiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15分钟
|
||||
max: 100, // 限制每个IP每15分钟最多100个请求
|
||||
message: '请求过于频繁,请稍后再试'
|
||||
});
|
||||
app.use(limiter);
|
||||
|
||||
// 解析请求体
|
||||
app.use(express.json({ limit: '10mb' }));
|
||||
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
|
||||
|
||||
// 静态文件服务
|
||||
app.use('/uploads', express.static('uploads'));
|
||||
|
||||
// 健康检查路由
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
timestamp: new Date().toISOString(),
|
||||
uptime: process.uptime()
|
||||
});
|
||||
});
|
||||
|
||||
// API路由
|
||||
app.use('/api/auth', require('../routes/auth'));
|
||||
app.use('/api/users', require('../routes/users'));
|
||||
app.use('/api/insurance', require('../routes/insurance'));
|
||||
app.use('/api/insurance-types', require('../routes/insuranceTypes'));
|
||||
app.use('/api/policies', require('../routes/policies'));
|
||||
app.use('/api/claims', require('../routes/claims'));
|
||||
app.use('/api/system', require('../routes/system'));
|
||||
|
||||
// API文档路由
|
||||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
|
||||
explorer: true,
|
||||
customCss: '.swagger-ui .topbar { display: none }',
|
||||
customSiteTitle: '保险端口系统 API文档'
|
||||
}));
|
||||
|
||||
// 404处理
|
||||
app.use('*', (req, res) => {
|
||||
res.status(404).json({
|
||||
code: 404,
|
||||
status: 'error',
|
||||
message: '接口不存在',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
// 全局错误处理
|
||||
app.use((err, req, res, next) => {
|
||||
console.error('全局错误:', err);
|
||||
|
||||
res.status(err.status || 500).json({
|
||||
code: err.status || 500,
|
||||
status: 'error',
|
||||
message: err.message || '服务器内部错误',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
const startServer = async () => {
|
||||
try {
|
||||
// 测试数据库连接
|
||||
const dbConnected = await testConnection();
|
||||
if (!dbConnected) {
|
||||
console.error('❌ 数据库连接失败,服务器启动中止');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Redis连接已移除
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 保险端口后端服务已启动`);
|
||||
console.log(`📍 服务地址: http://localhost:${PORT}`);
|
||||
console.log(`🌐 环境: ${process.env.NODE_ENV || 'development'}`);
|
||||
console.log(`⏰ 启动时间: ${new Date().toLocaleString()}`);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('❌ 服务器启动失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
// 优雅关闭
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\n🛑 正在关闭服务器...');
|
||||
await sequelize.close();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
// 启动应用
|
||||
startServer();
|
||||
|
||||
module.exports = app;
|
||||
Reference in New Issue
Block a user