-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (64 loc) · 1.93 KB
/
server.js
File metadata and controls
74 lines (64 loc) · 1.93 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const port = process.env.PORT || 9966;
// 静态文件服务配置
const staticOptions = {
maxAge: '1d', // 缓存1天
etag: false,
lastModified: false
};
// 直接 serve dist 目录,自动映射所有子目录
app.use(express.static(path.join(__dirname, 'dist'), staticOptions));
// 动态读取 dist 目录下的项目
const distPath = path.join(__dirname, 'dist');
let projects = [];
try {
if (fs.existsSync(distPath)) {
projects = fs.readdirSync(distPath)
.filter(item => fs.statSync(path.join(distPath, item)).isDirectory());
}
} catch (err) {
console.warn('Warning: Could not read dist directory:', err.message);
}
// SPA fallback - 为每个项目处理路由
projects.forEach(project => {
app.get(`/${project}/*`, (req, res) => {
res.sendFile(path.join(__dirname, 'dist', project, 'index.html'));
});
});
// 根路径重定向到主页面(可以选择一个默认项目)
app.get('/', (req, res) => {
const defaultProject = projects.length > 0 ? projects[0] : 'api';
res.redirect(`/${defaultProject}`);
});
// 健康检查端点
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// 404 处理
app.use((req, res) => {
const projectLinks = projects.map(project =>
`<li><a href="/${project}">/${project}</a></li>`
).join('');
res.status(404).send(`
<h1>404 - Page Not Found</h1>
<p>Available projects:</p>
<ul>
${projectLinks}
</ul>
`);
});
// 错误处理
app.use((err, req, res, next) => {
console.error('Error:', err);
res.status(500).json({ error: 'Internal Server Error' });
});
app.listen(port, '0.0.0.0', () => {
console.log(`Documentation server running on port ${port}`);
console.log('Available routes:');
projects.forEach(project => {
console.log(` /${project} -> dist/${project}`);
});
});