agent-publish-server 1.0.12 → 1.0.14
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/README.md +42 -32
- package/dist/server.js +28 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,37 +8,21 @@
|
|
|
8
8
|
npm install agent-publish-server -g
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## 使用步骤
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
agent-publish-server
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## 配置
|
|
18
|
-
|
|
19
|
-
### 配置文件路径
|
|
20
|
-
|
|
21
|
-
你可以通过以下方式指定配置文件路径:
|
|
22
|
-
|
|
23
|
-
1. 使用相对路径(相对于当前工作目录):
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
agent-publish-server -c ./agent_config.json
|
|
27
|
-
```
|
|
13
|
+
### 步骤 1:初始化配置文件
|
|
28
14
|
|
|
29
|
-
|
|
15
|
+
首先,初始化配置文件:
|
|
30
16
|
|
|
31
17
|
```bash
|
|
32
|
-
agent-publish-server
|
|
18
|
+
agent-publish-server init
|
|
33
19
|
```
|
|
34
20
|
|
|
35
|
-
|
|
21
|
+
这将在当前目录生成默认配置文件 `agent_config.json`。
|
|
36
22
|
|
|
37
|
-
|
|
38
|
-
agent-publish-server init
|
|
39
|
-
```
|
|
23
|
+
### 步骤 2:自定义配置(可选)
|
|
40
24
|
|
|
41
|
-
|
|
25
|
+
根据需要修改生成的 `agent_config.json` 配置文件。配置格式如下:
|
|
42
26
|
|
|
43
27
|
```json
|
|
44
28
|
{
|
|
@@ -58,13 +42,39 @@ agent-publish-server init
|
|
|
58
42
|
}
|
|
59
43
|
```
|
|
60
44
|
|
|
61
|
-
|
|
45
|
+
### 步骤 3:启动服务
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
agent-publish-server
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 配置选项
|
|
52
|
+
|
|
53
|
+
### 指定配置文件
|
|
54
|
+
|
|
55
|
+
你可以通过以下方式指定自定义配置文件路径:
|
|
56
|
+
|
|
57
|
+
1. 使用相对路径(相对于当前工作目录):
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
agent-publish-server -c ./agent_config.json
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
2. 使用绝对路径:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
agent-publish-server -c /path/to/agent_config.json
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
支持的配置文件参数:
|
|
70
|
+
|
|
71
|
+
- `-c`
|
|
72
|
+
- `--config`
|
|
73
|
+
- `--cf`
|
|
74
|
+
- `--config-file`
|
|
75
|
+
|
|
76
|
+
## 其他说明
|
|
62
77
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
> 4. 配置文件路径可以通过 --cf 来指定
|
|
67
|
-
> 5. 配置文件路径可以通过 --config-file 来指定
|
|
68
|
-
> 6. 适配 react vue 等前端框架
|
|
69
|
-
> 7. 使用 -v 或 --version 查看当前版本
|
|
70
|
-
> 8. 使用 init 命令初始化配置文件,将在当前目录生成 agent_config.json
|
|
78
|
+
- 适配 React、Vue 等前端框架
|
|
79
|
+
- 使用 `-v` 或 `--version` 查看当前版本
|
|
80
|
+
- 默认配置文件名为 `agent_config.json`
|
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) {
|