agent-publish-server 1.0.16 → 1.0.18
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 +68 -1
- package/dist/cli.js +0 -0
- package/dist/server.js +21 -1
- package/dist/types.d.ts +5 -0
- package/package.json +1 -1
- package/test-static-proxy.json +11 -0
package/README.md
CHANGED
|
@@ -63,6 +63,12 @@ agent-publish-server init
|
|
|
63
63
|
"^/api": ""
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
+
},
|
|
67
|
+
"staticProxy": {
|
|
68
|
+
"/sub_page": {
|
|
69
|
+
"target": "http://localhost:3000",
|
|
70
|
+
"changeOrigin": true
|
|
71
|
+
}
|
|
66
72
|
}
|
|
67
73
|
}
|
|
68
74
|
```
|
|
@@ -231,7 +237,7 @@ interface AgentConfig {
|
|
|
231
237
|
dir?: string; // 静态文件目录(默认:"./")
|
|
232
238
|
log?: boolean; // 启用访问日志(默认:true)
|
|
233
239
|
proxy?: {
|
|
234
|
-
// 代理配置
|
|
240
|
+
// API代理配置
|
|
235
241
|
[path: string]: {
|
|
236
242
|
target: string; // 目标URL
|
|
237
243
|
changeOrigin?: boolean; // 更改源头
|
|
@@ -241,9 +247,67 @@ interface AgentConfig {
|
|
|
241
247
|
};
|
|
242
248
|
};
|
|
243
249
|
};
|
|
250
|
+
staticProxy?: {
|
|
251
|
+
// 静态网页代理配置
|
|
252
|
+
[path: string]: {
|
|
253
|
+
target: string; // 目标URL
|
|
254
|
+
changeOrigin?: boolean; // 更改源头(默认:true)
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### 静态网页代理
|
|
261
|
+
|
|
262
|
+
除了API代理外,还支持静态网页代理功能,用于代理整个网站或应用。
|
|
263
|
+
|
|
264
|
+
#### 配置示例
|
|
265
|
+
|
|
266
|
+
```json
|
|
267
|
+
{
|
|
268
|
+
"staticProxy": {
|
|
269
|
+
"/sub_page": {
|
|
270
|
+
"target": "http://localhost:3000",
|
|
271
|
+
"changeOrigin": true
|
|
272
|
+
}
|
|
273
|
+
}
|
|
244
274
|
}
|
|
245
275
|
```
|
|
246
276
|
|
|
277
|
+
#### 功能特性
|
|
278
|
+
|
|
279
|
+
- **路径代理**:访问 `/sub_page` 会代理到目标服务器的根路径
|
|
280
|
+
- **自动路径重写**:自动移除代理路径前缀,确保目标服务器收到正确请求
|
|
281
|
+
- **优先级处理**:静态代理优先于静态文件服务,避免路径冲突
|
|
282
|
+
- **跨域支持**:默认启用 `changeOrigin` 选项
|
|
283
|
+
|
|
284
|
+
#### 与API代理组合使用
|
|
285
|
+
|
|
286
|
+
`staticProxy` 可以与 `proxy` 配置同时使用:
|
|
287
|
+
|
|
288
|
+
```json
|
|
289
|
+
{
|
|
290
|
+
"proxy": {
|
|
291
|
+
"/api": {
|
|
292
|
+
"target": "http://localhost:3000",
|
|
293
|
+
"changeOrigin": true,
|
|
294
|
+
"pathRewrite": { "^/api": "" }
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
"staticProxy": {
|
|
298
|
+
"/app": {
|
|
299
|
+
"target": "http://localhost:3001",
|
|
300
|
+
"changeOrigin": true
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
这样配置后:
|
|
307
|
+
- 访问 `/api/*` 会代理到 `http://localhost:3000/*`(API服务)
|
|
308
|
+
- 访问 `/app/*` 会代理到 `http://localhost:3001/*`(前端应用)
|
|
309
|
+
- 其他路径会访问本地静态文件
|
|
310
|
+
|
|
247
311
|
### 访问日志
|
|
248
312
|
|
|
249
313
|
访问日志默认启用,显示内容包括:
|
|
@@ -277,9 +341,12 @@ agent-publish-server --log false
|
|
|
277
341
|
- **前端测试**:快速静态文件服务,处理跨域问题
|
|
278
342
|
- **本地开发**:代理 API 调用到后端服务
|
|
279
343
|
- **SPA 部署**:为单页应用提供正确的路由支持
|
|
344
|
+
- **微前端架构**:通过静态代理整合多个前端应用
|
|
345
|
+
- **开发环境整合**:将多个本地服务统一到一个端口访问
|
|
280
346
|
|
|
281
347
|
### 版本历史
|
|
282
348
|
|
|
349
|
+
- **v1.0.17**: 新增静态网页代理功能(staticProxy),支持与API代理同时使用
|
|
283
350
|
- **v1.0.16**: 新增交互式配置、访问日志、路径验证功能
|
|
284
351
|
- **v1.0.15**: 基础代理和静态文件服务功能
|
|
285
352
|
|
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/server.js
CHANGED
|
@@ -30,12 +30,24 @@ function createServer(config) {
|
|
|
30
30
|
next();
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
|
-
//
|
|
33
|
+
// 配置API代理
|
|
34
34
|
if (config.proxy) {
|
|
35
35
|
Object.entries(config.proxy).forEach(([path, proxyConfig]) => {
|
|
36
36
|
app.use(path, (0, http_proxy_middleware_1.createProxyMiddleware)(proxyConfig));
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
|
+
// 配置静态网页代理
|
|
40
|
+
if (config.staticProxy) {
|
|
41
|
+
Object.entries(config.staticProxy).forEach(([path, staticProxyConfig]) => {
|
|
42
|
+
app.use(path, (0, http_proxy_middleware_1.createProxyMiddleware)({
|
|
43
|
+
target: staticProxyConfig.target,
|
|
44
|
+
changeOrigin: staticProxyConfig.changeOrigin !== false, // 默认为true
|
|
45
|
+
pathRewrite: {
|
|
46
|
+
[`^${path}`]: '' // 移除代理路径前缀
|
|
47
|
+
}
|
|
48
|
+
}));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
39
51
|
// 配置静态文件服务
|
|
40
52
|
app.use(express_1.default.static(staticDir));
|
|
41
53
|
// 添加SPA应用的回退路由处理
|
|
@@ -45,6 +57,14 @@ function createServer(config) {
|
|
|
45
57
|
if (req.path.startsWith('/api/')) {
|
|
46
58
|
return next();
|
|
47
59
|
}
|
|
60
|
+
// 排除静态代理路径
|
|
61
|
+
if (config.staticProxy) {
|
|
62
|
+
for (const proxyPath of Object.keys(config.staticProxy)) {
|
|
63
|
+
if (req.path.startsWith(proxyPath)) {
|
|
64
|
+
return next();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
48
68
|
const filePath = path_1.default.join(process.cwd(), staticDir, req.path);
|
|
49
69
|
// 检查是否为目录
|
|
50
70
|
if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isDirectory()) {
|
package/dist/types.d.ts
CHANGED
|
@@ -3,10 +3,15 @@ export interface ProxyConfig {
|
|
|
3
3
|
changeOrigin?: boolean;
|
|
4
4
|
pathRewrite?: Record<string, string>;
|
|
5
5
|
}
|
|
6
|
+
export interface StaticProxyConfig {
|
|
7
|
+
target: string;
|
|
8
|
+
changeOrigin?: boolean;
|
|
9
|
+
}
|
|
6
10
|
export interface AgentConfig {
|
|
7
11
|
port?: number;
|
|
8
12
|
dir?: string;
|
|
9
13
|
proxy?: Record<string, ProxyConfig>;
|
|
14
|
+
staticProxy?: Record<string, StaticProxyConfig>;
|
|
10
15
|
log?: boolean;
|
|
11
16
|
}
|
|
12
17
|
export interface CommandOptions {
|
package/package.json
CHANGED