Merge remote-tracking branch 'origin/main'
# Conflicts: # backend/api/server.js
This commit is contained in:
@@ -9,7 +9,7 @@ dotenv.config();
|
||||
|
||||
// 创建Express应用
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3350; // 生产环境使用3350端口
|
||||
const PORT = process.env.PORT || 8000;
|
||||
|
||||
// 中间件
|
||||
app.use(helmet()); // 安全头部
|
||||
@@ -20,7 +20,7 @@ app.use(express.urlencoded({ extended: true, limit: '10mb' })); // URL编码解
|
||||
// 速率限制
|
||||
const limiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15分钟
|
||||
max: 1000, // 生产环境提高限制
|
||||
max: 100, // 限制每个IP 15分钟内最多100个请求
|
||||
message: '请求过于频繁,请稍后再试'
|
||||
});
|
||||
app.use(limiter);
|
||||
@@ -30,30 +30,14 @@ app.get('/', (req, res) => {
|
||||
res.json({
|
||||
message: '欢迎使用锡林郭勒盟地区智慧养殖产业平台API服务',
|
||||
version: '1.0.0',
|
||||
environment: process.env.NODE_ENV || 'development',
|
||||
timestamp: new Date().toISOString(),
|
||||
docs: 'https://xlapi.jiebanke.com/docs'
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'OK',
|
||||
timestamp: new Date().toISOString(),
|
||||
uptime: process.uptime(),
|
||||
memory: process.memoryUsage()
|
||||
});
|
||||
});
|
||||
|
||||
// API信息端点
|
||||
app.get('/api/info', (req, res) => {
|
||||
res.json({
|
||||
name: 'xlxumu-api',
|
||||
version: '1.0.0',
|
||||
environment: process.env.NODE_ENV,
|
||||
port: PORT,
|
||||
node_version: process.version,
|
||||
platform: process.platform
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
@@ -168,46 +152,9 @@ app.get('/api/v1/dashboard/map/region/:regionId', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 错误处理中间件
|
||||
app.use((err, req, res, next) => {
|
||||
console.error('服务器错误:', err.stack);
|
||||
res.status(500).json({
|
||||
error: '内部服务器错误',
|
||||
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong!'
|
||||
});
|
||||
});
|
||||
|
||||
// 404处理
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({
|
||||
error: '接口未找到',
|
||||
path: req.path,
|
||||
method: req.method
|
||||
});
|
||||
});
|
||||
|
||||
// 优雅关机处理
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n收到SIGINT信号,正在优雅关闭服务器...');
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
console.log('\n收到SIGTERM信号,正在优雅关闭服务器...');
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
const server = app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`🚀 API服务器正在运行:`);
|
||||
console.log(` 📍 本地: http://localhost:${PORT}`);
|
||||
console.log(` 🌐 网络: http://0.0.0.0:${PORT}`);
|
||||
console.log(` 🏷️ 环境: ${process.env.NODE_ENV || 'development'}`);
|
||||
console.log(` ⏰ 启动时间: ${new Date().toLocaleString()}`);
|
||||
app.listen(PORT, () => {
|
||||
console.log(`API服务器正在端口 ${PORT} 上运行`);
|
||||
});
|
||||
|
||||
// 设置超时
|
||||
server.timeout = 60000;
|
||||
server.keepAliveTimeout = 5000;
|
||||
|
||||
module.exports = app;
|
||||
Reference in New Issue
Block a user