-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (44 loc) · 1.59 KB
/
server.js
File metadata and controls
53 lines (44 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { createServer } = require("http");
const { parse } = require("url");
const next = require("next");
const { Server } = require("socket.io");
const express = require("express");
const socketHandler = require("./server/state/socketHandler");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
const httpServer = createServer(server);
const io = new Server(httpServer);
// 初始化 Socket.io 逻辑 (包含管理员烧录)
socketHandler(io);
// 1. 静态资源优先级处理 (无需通配符字符串)
server.use((req, res, next) => {
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;
// 显式允许 Next.js 内部资源
if (pathname.startsWith('/_next') || pathname.startsWith('/static')) {
return handle(req, res, parsedUrl);
}
next();
});
// 2. 静态文件目录
server.use(express.static('public'));
// 3. 核心修复:路由全接管
// 不再使用 '*' 字符串,而是使用 .use() 不带路径参数,
// 这样它会捕获所有到达这里的请求。
server.use((req, res) => {
// 排除 API
const parsedUrl = parse(req.url, true);
return handle(req, res, parsedUrl);
});
const PORT = process.env.PORT || 3000;
httpServer.listen(PORT, (err) => {
if (err) throw err;
console.log(`---`);
console.log(`> [Aether Core] Mode: ${dev ? "DEVELOPMENT" : "PRODUCTION"}`);
console.log(`> [Aether Core] URL: http://localhost:${PORT}`);
console.log(`---`);
});
});