提交
This commit is contained in:
48
server.js
48
server.js
@@ -5,11 +5,8 @@ const express = require('express');
|
||||
const cors = require('cors');
|
||||
const bodyParser = require('body-parser');
|
||||
const path = require('path');
|
||||
const mysql = require('mysql2/promise');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const helmet = require('helmet');
|
||||
const swaggerUi = require('swagger-ui-express');
|
||||
const swaggerSpecs = require('./swagger');
|
||||
const { initDB, getDB, dbConfig } = require('./database');
|
||||
const { logger } = require('./config/logger');
|
||||
const { errorHandler, notFound } = require('./middleware/errorHandler');
|
||||
@@ -88,24 +85,6 @@ const limiter = rateLimit({
|
||||
}
|
||||
});
|
||||
app.use('/api', limiter);
|
||||
|
||||
// 静态文件服务 - 为代理后台前端提供服务
|
||||
app.use('/agent-admin', express.static(path.join(__dirname, 'agent-admin/dist'), {
|
||||
setHeaders: (res, filePath) => {
|
||||
// 设置CORS头部
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
||||
|
||||
// 设置缓存策略
|
||||
if (filePath.includes('.js') || filePath.includes('.css')) {
|
||||
res.setHeader('Cache-Control', 'public, max-age=31536000'); // 1年缓存
|
||||
} else {
|
||||
res.setHeader('Cache-Control', 'public, max-age=86400'); // 1天缓存
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// 上传文件静态服务
|
||||
app.use('/uploads', express.static(path.join(__dirname, 'uploads'), {
|
||||
setHeaders: (res, filePath) => {
|
||||
@@ -114,9 +93,6 @@ app.use('/uploads', express.static(path.join(__dirname, 'uploads'), {
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
||||
}
|
||||
}));
|
||||
|
||||
// 代理后端共用现有数据库,无需初始化
|
||||
|
||||
// API路由 - 代理专用路由
|
||||
app.use('/api/auth', require('./routes/auth'));
|
||||
// 添加代理认证路由别名,兼容前端请求
|
||||
@@ -128,27 +104,7 @@ app.use('/api/commissions', require('./routes/commissions'));
|
||||
app.use('/api/upload', require('./routes/upload'));
|
||||
app.use('/api/captcha', require('./routes/captcha')); // 验证码路由
|
||||
|
||||
// Swagger API 文档
|
||||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs, {
|
||||
explorer: true,
|
||||
customCss: '.swagger-ui .topbar { display: none }',
|
||||
customSiteTitle: '炬融圈代理后台API文档'
|
||||
}));
|
||||
|
||||
// 代理后台首页路由
|
||||
app.get('/', (req, res) => {
|
||||
res.json({
|
||||
message: '炬融圈代理后台API服务',
|
||||
version: '1.0.0',
|
||||
status: 'running',
|
||||
docs: '/api-docs'
|
||||
});
|
||||
});
|
||||
|
||||
// 代理后台前端路由
|
||||
app.get('/agent-admin*', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'agent-admin/dist/index.html'));
|
||||
});
|
||||
|
||||
// 404处理
|
||||
app.use(notFound);
|
||||
@@ -170,10 +126,6 @@ app.listen(PORT, async () => {
|
||||
// 初始化数据库连接
|
||||
await initDB();
|
||||
console.log(`代理后台API服务器运行在端口 ${PORT}`);
|
||||
console.log(`代理后台管理界面: http://localhost:${PORT}/agent-admin`);
|
||||
|
||||
// 代理后端共用现有数据库,无需初始化表结构
|
||||
|
||||
} catch (error) {
|
||||
console.error('服务器启动失败:', error);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user