require('dotenv').config(); const express = require('express'); const cors = require('cors'); const roomsRouter = require('./routes/rooms'); const srsRouter = require('./routes/srs'); const errorHandler = require('./middleware/errorHandler'); const app = express(); const PORT = process.env.PORT || 3001; // 中间件 app.use(cors({ origin: process.env.CLIENT_URL || 'http://localhost:3000', credentials: true })); app.use(express.json()); app.use(express.urlencoded({ extended: true })); // 路由 app.use('/api/rooms', roomsRouter); app.use('/api/srs', srsRouter); // 健康检查 app.get('/health', (req, res) => { res.json({ status: 'ok', timestamp: new Date().toISOString() }); }); // 错误处理 app.use(errorHandler); // 启动服务 app.listen(PORT, () => { console.log(`API 服务运行在 http://localhost:${PORT}`); console.log(`SRS RTMP: rtmp://${process.env.SRS_HOST || 'localhost'}:${process.env.SRS_RTMP_PORT || 1935}/live/{streamKey}`); console.log(`SRS HTTP: http://${process.env.SRS_HOST || 'localhost'}:${process.env.SRS_HTTP_PORT || 8080}/live/{streamKey}.flv`); }); module.exports = app;