40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
const express = require('express');
|
|
const http = require('http'); // 引入 HTTP 模块
|
|
const cors = require('cors');
|
|
const authRoutes = require('./auth');
|
|
const { initWebSocket } = require('./websocket'); // 引入 WebSocket 初始化函数
|
|
const { onUserDisconnected, router: waitingRoomRoutes } = require('./waiting_room');
|
|
const { router: gameRouter } = require('./game');
|
|
|
|
|
|
const app = express();
|
|
|
|
// 配置 CORS
|
|
app.use(cors({
|
|
origin: ['http://127.0.0.1:8080', 'http://localhost:8080', 'http://player1.localhost:8080', 'http://player2.localhost:8080', 'http://player3.localhost:8080', 'http://player4.localhost:8080', 'http://player5.localhost:8080', 'http://player6.localhost:8080', 'http://player7.localhost:8080', 'http://player8.localhost:8080', 'http://werewolf.xn--xhq44jb2fzpc.com'], // 允许的前端地址
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE'], // 允许的 HTTP 方法
|
|
credentials: true, // 允许携带 Cookies
|
|
}));
|
|
|
|
// 中间件
|
|
app.use(express.json()); // 解析 JSON 请求体
|
|
|
|
// 路由
|
|
app.use('/auth', authRoutes); // 挂载路由到 /auth 路径
|
|
|
|
app.use('/room', waitingRoomRoutes);
|
|
|
|
app.use('/game', gameRouter);
|
|
|
|
// 创建 HTTP 服务器
|
|
const server = http.createServer(app);
|
|
|
|
// 初始化 WebSocket 服务
|
|
initWebSocket(server, onUserDisconnected);
|
|
|
|
// 启动 HTTP 和 WebSocket 服务
|
|
const PORT = 7570;
|
|
server.listen(PORT, () => {
|
|
console.log(`服务器运行在 http://localhost:${PORT}`);
|
|
});
|