-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathserver.js
51 lines (40 loc) · 1.36 KB
/
server.js
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
// server.js
import express from 'express';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
dotenv.config(); // 加载 .env 文件中的环境变量
const app = express();
// 获取当前文件的目录名
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 中间件:解析 JSON 请求体
app.use(express.json());
// 引入路由
import authRouter from './api/local/auth.js';
import apiRouter from './api/local/index.js';
import aliveRouter from './api/local/alive.js';
// 设置后端接口路由,位于 `/api` 路径下
app.use('/api/alive', aliveRouter);
app.use('/api/auth', authRouter);
app.use('/api', apiRouter);
// 设置静态文件目录,位于根路径 `/`
app.use(express.static(path.join(__dirname, 'dist')));
// 处理 SPA 前端路由
app.get('*', (req, res) => {
if (!req.path.startsWith('/api')) {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
} else {
res.status(404).json({ message: 'Not Found' });
}
});
// 全局错误处理中间件
app.use((err, req, res, next) => {
console.error('全局错误处理捕获到错误:', err);
res.status(500).json({ message: 'Internal Server Error' });
});
// 启动服务器
const PORT = process.env.PORT || 13000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});