49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const helmet = require('helmet');
|
|
const dotenv = require('dotenv');
|
|
const rateLimit = require('express-rate-limit');
|
|
|
|
// 加载环境变量
|
|
dotenv.config();
|
|
|
|
// 创建Express应用
|
|
const app = express();
|
|
const PORT = process.env.PORT || 8000;
|
|
|
|
// 中间件
|
|
app.use(helmet()); // 安全头部
|
|
app.use(cors()); // 跨域支持
|
|
app.use(express.json({ limit: '10mb' })); // JSON解析
|
|
app.use(express.urlencoded({ extended: true, limit: '10mb' })); // URL编码解析
|
|
|
|
// 速率限制
|
|
const limiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000, // 15分钟
|
|
max: 100, // 限制每个IP 15分钟内最多100个请求
|
|
message: '请求过于频繁,请稍后再试'
|
|
});
|
|
app.use(limiter);
|
|
|
|
// 基础路由
|
|
app.get('/', (req, res) => {
|
|
res.json({
|
|
message: '欢迎使用锡林郭勒盟地区智慧养殖产业平台API服务',
|
|
version: '1.0.0',
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
app.get('/health', (req, res) => {
|
|
res.json({
|
|
status: 'OK',
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
// 启动服务器
|
|
app.listen(PORT, () => {
|
|
console.log(`API服务器正在端口 ${PORT} 上运行`);
|
|
});
|
|
|
|
module.exports = app; |