agent-publish-server 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server.js +28 -2
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -7,16 +7,42 @@ exports.createServer = createServer;
|
|
|
7
7
|
exports.startServer = startServer;
|
|
8
8
|
const express_1 = __importDefault(require("express"));
|
|
9
9
|
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
12
|
function createServer(config) {
|
|
11
13
|
const app = (0, express_1.default)();
|
|
12
|
-
|
|
13
|
-
app.use(express_1.default.static(config.dir || './'));
|
|
14
|
+
const staticDir = config.dir || './';
|
|
14
15
|
// 配置代理
|
|
15
16
|
if (config.proxy) {
|
|
16
17
|
Object.entries(config.proxy).forEach(([path, proxyConfig]) => {
|
|
17
18
|
app.use(path, (0, http_proxy_middleware_1.createProxyMiddleware)(proxyConfig));
|
|
18
19
|
});
|
|
19
20
|
}
|
|
21
|
+
// 配置静态文件服务
|
|
22
|
+
app.use(express_1.default.static(staticDir));
|
|
23
|
+
// 添加SPA应用的回退路由处理
|
|
24
|
+
// 只有当实际文件不存在时才回退到index.html
|
|
25
|
+
app.use((req, res, next) => {
|
|
26
|
+
// 排除API和已处理的代理请求
|
|
27
|
+
if (req.path.startsWith('/api/')) {
|
|
28
|
+
return next();
|
|
29
|
+
}
|
|
30
|
+
const filePath = path_1.default.join(process.cwd(), staticDir, req.path);
|
|
31
|
+
// 检查是否为目录
|
|
32
|
+
if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isDirectory()) {
|
|
33
|
+
// 检查目录中是否有index.html
|
|
34
|
+
const indexPath = path_1.default.join(filePath, 'index.html');
|
|
35
|
+
if (fs_1.default.existsSync(indexPath)) {
|
|
36
|
+
return res.sendFile(indexPath);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// 检查完整路径(如果是文件)
|
|
40
|
+
if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isFile()) {
|
|
41
|
+
return res.sendFile(filePath);
|
|
42
|
+
}
|
|
43
|
+
// 如果路径不存在,回退到index.html
|
|
44
|
+
res.sendFile(path_1.default.join(process.cwd(), staticDir, 'index.html'));
|
|
45
|
+
});
|
|
20
46
|
return app;
|
|
21
47
|
}
|
|
22
48
|
function startServer(config) {
|