agent-publish-server 1.0.19 → 1.0.23

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 CHANGED
@@ -1,166 +1,22 @@
1
- # agent-publish-server
1
+ # Agent Publish Server
2
2
 
3
- [English](#english) | [中文](#chinese)
3
+ [![npm version](https://badge.fury.io/js/agent-publish-server.svg)](https://badge.fury.io/js/agent-publish-server)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
5
 
5
- ---
6
+ 基于 Node.js + Express + http-proxy-middleware 的前端服务启动插件,提供强大的代理和静态文件服务能力。
6
7
 
7
- ## <a id="english"></a>English
8
+ [English](./README_EN.md) | 中文
8
9
 
9
- > A frontend service startup plugin based on nodejs+express+http-proxy-middleware with proxy support
10
+ ## 功能特性
10
11
 
11
- ### Features
12
-
13
- - 🚀 **Quick Start**: One-command startup for development server
14
- - 🔄 **Proxy Support**: Built-in proxy middleware for API forwarding
15
- - 📁 **Static File Serving**: Serve static files from any directory
16
- - ⚙️ **Interactive Configuration**: Easy setup with interactive CLI
17
- - 📝 **Access Logging**: Optional request logging with timestamps
18
- - 🛡️ **Path Validation**: Automatic project path validation
19
- - 🎯 **SPA Support**: Single Page Application routing support
20
-
21
- ### Installation
22
-
23
- ```bash
24
- npm install agent-publish-server -g
25
- ```
26
-
27
- ### Quick Start
28
-
29
- #### Method 1: Interactive Configuration (Recommended)
30
-
31
- ```bash
32
- agent-publish-server -wp
33
- ```
34
-
35
- This will guide you through an interactive setup:
36
-
37
- 1. Enter port number
38
- 2. Enter project directory (after build)
39
- 3. Enter proxy configuration (format: `/api:http://localhost:3000`)
40
- 4. Choose to continue adding proxies (1: continue, 0: finish)
41
- 5. Generate `agent_config.json` and get startup command
42
-
43
- #### Method 2: Manual Configuration
44
-
45
- 1. **Initialize configuration file:**
46
-
47
- ```bash
48
- agent-publish-server init
49
- ```
50
-
51
- 2. **Edit the generated `agent_config.json`:**
52
-
53
- ```json
54
- {
55
- "port": 8080,
56
- "dir": "./",
57
- "log": true,
58
- "proxy": {
59
- "/api": {
60
- "target": "http://localhost:3000",
61
- "changeOrigin": true,
62
- "pathRewrite": {
63
- "^/api": ""
64
- }
65
- }
66
- },
67
- "staticProxy": {
68
- "/sub_page": {
69
- "target": "http://localhost:3000",
70
- "changeOrigin": true
71
- }
72
- }
73
- }
74
- ```
75
-
76
- 3. **Start the server:**
77
-
78
- ```bash
79
- agent-publish-server -c ./agent_config.json
80
- ```
81
-
82
- ### Command Line Options
83
-
84
- | Option | Description | Example |
85
- | --------------------- | ------------------------------- | --------------------------------------- |
86
- | `-wp, --write-proxy` | Interactive proxy configuration | `agent-publish-server -wp` |
87
- | `-c, --config <path>` | Specify configuration file path | `agent-publish-server -c ./config.json` |
88
- | `-p, --port <number>` | Override port number | `agent-publish-server -p 3000` |
89
- | `-d, --dir <path>` | Override static directory | `agent-publish-server -d ./dist` |
90
- | `--log <boolean>` | Enable/disable access logging | `agent-publish-server --log false` |
91
- | `-v, --version` | Show version | `agent-publish-server -v` |
92
- | `init` | Initialize configuration file | `agent-publish-server init` |
93
-
94
- ### Configuration Schema
95
-
96
- ```typescript
97
- interface AgentConfig {
98
- port?: number; // Server port (default: 8080)
99
- dir?: string; // Static files directory (default: "./")
100
- log?: boolean; // Enable access logging (default: true)
101
- proxy?: {
102
- // Proxy configurations
103
- [path: string]: {
104
- target: string; // Target URL
105
- changeOrigin?: boolean; // Change origin header
106
- pathRewrite?: {
107
- // Path rewrite rules
108
- [pattern: string]: string;
109
- };
110
- };
111
- };
112
- }
113
- ```
114
-
115
- ### Access Logging
116
-
117
- Access logging is enabled by default and shows:
118
-
119
- - Timestamp (ISO format)
120
- - Client IP address
121
- - HTTP method and URL
122
- - User agent string
123
-
124
- Example log output:
125
-
126
- ```
127
- [2025-08-27T11:02:48.854Z] ::1 "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
128
- ```
129
-
130
- To disable logging:
131
-
132
- ```bash
133
- agent-publish-server --log false
134
- ```
135
-
136
- ### Error Handling
137
-
138
- - **Invalid project path**: Shows "项目路径错误" (Project path error) and exits
139
- - **Invalid port**: Validates port numbers (1-65535)
140
- - **Missing configuration**: Falls back to defaults
141
-
142
- ### Use Cases
143
-
144
- - **React/Vue Development**: Serve built applications with API proxying
145
- - **Frontend Testing**: Quick static file serving with CORS handling
146
- - **Local Development**: Proxy API calls to backend services
147
- - **SPA Deployment**: Proper routing support for single-page applications
148
-
149
- ---
150
-
151
- ## <a id="chinese"></a>中文
152
-
153
- > 这是一个基于 nodejs+express+http-proxy-middleware 的一款可支持代理的前端服务启动插件
154
-
155
- ### 功能特性
156
-
157
- - 🚀 **快速启动**: 一键启动开发服务器
158
- - 🔄 **代理支持**: 内置代理中间件,支持 API 转发
159
- - 📁 **静态文件服务**: 支持任意目录的静态文件托管
160
- - ⚙️ **交互式配置**: 通过问答方式轻松配置
161
- - 📝 **访问日志**: 可选的请求日志记录功能
162
- - 🛡️ **路径验证**: 自动验证项目路径有效性
163
- - 🎯 **SPA 支持**: 单页应用路由支持
12
+ - 🚀 **快速启动**:一键启动,支持交互式配置
13
+ - 🔄 **API 代理**:灵活的 API 请求代理,支持跨域处理
14
+ - 📁 **静态代理**:支持静态文件服务和 HTTP 服务代理两种模式
15
+ - 🌐 **双重模式**:静态文件代理和 HTTP 服务代理可同时使用
16
+ - 📝 **访问日志**:实时请求日志记录和监控
17
+ - **路径验证**:自动路径存在性检查和验证
18
+ - 🛠️ **CLI 支持**:命令行工具,易于集成到构建流程
19
+ - 📋 **配置文件**:支持 JSON 配置文件,灵活配置各种代理规则
164
20
 
165
21
  ### 安装
166
22
 
@@ -250,8 +106,9 @@ interface AgentConfig {
250
106
  staticProxy?: {
251
107
  // 静态网页代理配置
252
108
  [path: string]: {
253
- target: string; // 目标URL
254
- changeOrigin?: boolean; // 更改源头(默认:true
109
+ target: string; // 目标URL或本地文件路径
110
+ type?: "http" | "static"; // 代理类型(默认:http
111
+ changeOrigin?: boolean; // 更改源头,仅http类型有效(默认:true)
255
112
  };
256
113
  };
257
114
  }
@@ -259,15 +116,31 @@ interface AgentConfig {
259
116
 
260
117
  ### 静态网页代理
261
118
 
262
- 除了API代理外,还支持静态网页代理功能,用于代理整个网站或应用。
119
+ 除了 API 代理外,还支持静态网页代理功能,用于代理整个网站或应用。
263
120
 
264
121
  #### 配置示例
265
122
 
123
+ **静态文件代理:**
124
+
266
125
  ```json
267
126
  {
268
127
  "staticProxy": {
269
128
  "/sub_page": {
129
+ "target": "./dist",
130
+ "type": "static"
131
+ }
132
+ }
133
+ }
134
+ ```
135
+
136
+ **HTTP 服务代理:**
137
+
138
+ ```json
139
+ {
140
+ "staticProxy": {
141
+ "/app": {
270
142
  "target": "http://localhost:3000",
143
+ "type": "http",
271
144
  "changeOrigin": true
272
145
  }
273
146
  }
@@ -276,12 +149,14 @@ interface AgentConfig {
276
149
 
277
150
  #### 功能特性
278
151
 
279
- - **路径代理**:访问 `/sub_page` 会代理到目标服务器的根路径
280
- - **自动路径重写**:自动移除代理路径前缀,确保目标服务器收到正确请求
281
- - **优先级处理**:静态代理优先于静态文件服务,避免路径冲突
282
- - **跨域支持**:默认启用 `changeOrigin` 选项
152
+ - **双重代理模式**:支持静态文件代理和 HTTP 服务代理
153
+ - **静态文件代理**:直接代理到本地文件系统目录
154
+ - **HTTP 服务代理**:代理到远程 HTTP 服务,支持路径重写
155
+ - **自动路径处理**:自动处理代理路径前缀
156
+ - **优先级处理**:静态代理优先于默认静态文件服务
157
+ - **路径验证**:自动检查静态文件路径是否存在
283
158
 
284
- #### 与API代理组合使用
159
+ #### 与 API 代理组合使用
285
160
 
286
161
  `staticProxy` 可以与 `proxy` 配置同时使用:
287
162
 
@@ -295,8 +170,13 @@ interface AgentConfig {
295
170
  }
296
171
  },
297
172
  "staticProxy": {
298
- "/app": {
173
+ "/dist_files": {
174
+ "target": "./dist",
175
+ "type": "static"
176
+ },
177
+ "/remote_app": {
299
178
  "target": "http://localhost:3001",
179
+ "type": "http",
300
180
  "changeOrigin": true
301
181
  }
302
182
  }
@@ -304,9 +184,11 @@ interface AgentConfig {
304
184
  ```
305
185
 
306
186
  这样配置后:
307
- - 访问 `/api/*` 会代理到 `http://localhost:3000/*`(API服务)
308
- - 访问 `/app/*` 会代理到 `http://localhost:3001/*`(前端应用)
309
- - 其他路径会访问本地静态文件
187
+
188
+ - 访问 `/api/*` 会代理到 `http://localhost:3000/*`(API 服务)
189
+ - 访问 `/dist_files/*` 会访问本地 `./dist` 目录下的静态文件
190
+ - 访问 `/remote_app/*` 会代理到 `http://localhost:3001/*`(远程应用)
191
+ - 其他路径会访问默认静态文件目录
310
192
 
311
193
  ### 访问日志
312
194
 
@@ -346,7 +228,10 @@ agent-publish-server --log false
346
228
 
347
229
  ### 版本历史
348
230
 
349
- - **v1.0.17**: 新增静态网页代理功能(staticProxy),支持与API代理同时使用
231
+ - **v1.0.23**: 完善双语文档支持,优化package.json关键词,提升npm包曝光度
232
+ - **v1.0.22**: 优化和完善 staticProxy 功能,提升稳定性
233
+ - **v1.0.18**: 增强 staticProxy 功能,支持静态文件代理和 HTTP 服务代理两种模式
234
+ - **v1.0.17**: 新增静态网页代理功能(staticProxy),支持与 API 代理同时使用
350
235
  - **v1.0.16**: 新增交互式配置、访问日志、路径验证功能
351
236
  - **v1.0.15**: 基础代理和静态文件服务功能
352
237
 
package/README_EN.md ADDED
@@ -0,0 +1,248 @@
1
+ # Agent Publish Server
2
+
3
+ [![npm version](https://badge.fury.io/js/agent-publish-server.svg)](https://badge.fury.io/js/agent-publish-server)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A frontend service startup plugin based on Node.js + Express + http-proxy-middleware, providing powerful proxy and static file serving capabilities.
7
+
8
+ English | [中文](./README.md)
9
+
10
+ ## Features
11
+
12
+ - 🚀 **Quick Start**: One-click startup with interactive configuration
13
+ - 🔄 **API Proxy**: Flexible API request proxying with CORS support
14
+ - 📁 **Static Proxy**: Support for both static file serving and HTTP service proxying
15
+ - 🌐 **Dual Mode**: Static file proxy and HTTP service proxy can be used simultaneously
16
+ - 📝 **Access Logging**: Real-time request logging and monitoring
17
+ - ⚡ **Path Validation**: Automatic path existence checking and validation
18
+ - 🛠️ **CLI Support**: Command-line interface for easy integration
19
+ - 📋 **Configuration Files**: Support for JSON configuration files
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install -g agent-publish-server
25
+ ```
26
+
27
+ ## Quick Start
28
+
29
+ ### Interactive Mode
30
+
31
+ ```bash
32
+ agent-publish-server
33
+ ```
34
+
35
+ Follow the interactive prompts to configure your server.
36
+
37
+ ### Configuration File Mode
38
+
39
+ ```bash
40
+ agent-publish-server -c config.json
41
+ ```
42
+
43
+ ## Configuration
44
+
45
+ ### Basic Configuration Example
46
+
47
+ ```json
48
+ {
49
+ "port": 3000,
50
+ "dir": "./dist",
51
+ "proxy": {
52
+ "/api": {
53
+ "target": "http://localhost:8080",
54
+ "changeOrigin": true,
55
+ "pathRewrite": {
56
+ "^/api": ""
57
+ }
58
+ }
59
+ },
60
+ "staticProxy": {
61
+ "/assets": {
62
+ "target": "./static",
63
+ "type": "static"
64
+ },
65
+ "/app": {
66
+ "target": "http://localhost:4000",
67
+ "type": "http",
68
+ "changeOrigin": true
69
+ }
70
+ }
71
+ }
72
+ ```
73
+
74
+ ### Configuration Options
75
+
76
+ #### Server Configuration
77
+
78
+ - **port**: Server port (default: 3000)
79
+ - **dir**: Static file directory (default: current directory)
80
+ - **proxy**: API proxy configuration
81
+ - **staticProxy**: Static proxy configuration
82
+
83
+ #### API Proxy Configuration (ProxyConfig)
84
+
85
+ ```typescript
86
+ interface ProxyConfig {
87
+ target: string; // Target server URL
88
+ changeOrigin?: boolean; // Change origin header
89
+ pathRewrite?: Record<string, string>; // Path rewriting rules
90
+ }
91
+ ```
92
+
93
+ #### Static Proxy Configuration (StaticProxyConfig)
94
+
95
+ ```typescript
96
+ interface StaticProxyConfig {
97
+ target: string; // Target URL or local file path
98
+ type?: "http" | "static"; // Proxy type: http service or static files
99
+ changeOrigin?: boolean; // Change origin header (only effective for http type)
100
+ }
101
+ ```
102
+
103
+ ## Static Proxy
104
+
105
+ The static proxy feature supports two modes:
106
+
107
+ ### 1. Static File Proxy
108
+
109
+ Proxy requests to local file system directories:
110
+
111
+ ```json
112
+ {
113
+ "staticProxy": {
114
+ "/assets": {
115
+ "target": "./dist",
116
+ "type": "static"
117
+ }
118
+ }
119
+ }
120
+ ```
121
+
122
+ **Access**: `http://localhost:3000/assets/index.html` → `./dist/index.html`
123
+
124
+ ### 2. HTTP Service Proxy
125
+
126
+ Proxy requests to remote HTTP services:
127
+
128
+ ```json
129
+ {
130
+ "staticProxy": {
131
+ "/app": {
132
+ "target": "http://localhost:4000",
133
+ "type": "http",
134
+ "changeOrigin": true
135
+ }
136
+ }
137
+ }
138
+ ```
139
+
140
+ **Access**: `http://localhost:3000/app/page` → `http://localhost:4000/page`
141
+
142
+ ### Features
143
+
144
+ - **Dual Proxy Mode**: Static file proxy + HTTP service proxy
145
+ - **Path Validation**: Automatic checking of static file path existence
146
+ - **Automatic Path Rewriting**: Smart path processing and forwarding
147
+ - **Priority Handling**: Static proxy takes precedence over default static file serving
148
+ - **Backward Compatibility**: Fully compatible with previous versions
149
+
150
+ ## Combined Usage with API Proxy
151
+
152
+ ```json
153
+ {
154
+ "port": 3000,
155
+ "dir": "./public",
156
+ "proxy": {
157
+ "/api": {
158
+ "target": "http://localhost:8080",
159
+ "changeOrigin": true,
160
+ "pathRewrite": {
161
+ "^/api": ""
162
+ }
163
+ }
164
+ },
165
+ "staticProxy": {
166
+ "/admin": {
167
+ "target": "./admin-dist",
168
+ "type": "static"
169
+ },
170
+ "/mobile": {
171
+ "target": "http://localhost:4000",
172
+ "type": "http",
173
+ "changeOrigin": true
174
+ }
175
+ }
176
+ }
177
+ ```
178
+
179
+ **Access Examples**:
180
+
181
+ - API requests: `http://localhost:3000/api/users` → `http://localhost:8080/users`
182
+ - Admin panel: `http://localhost:3000/admin/index.html` → `./admin-dist/index.html`
183
+ - Mobile app: `http://localhost:3000/mobile/page` → `http://localhost:4000/page`
184
+ - Default static: `http://localhost:3000/index.html` → `./public/index.html`
185
+
186
+ ## Use Cases
187
+
188
+ - **Single Page Applications**: Serve SPA with API proxy configuration
189
+ - **Micro-frontend Architecture**: Integrate multiple frontend applications through static proxy
190
+ - **Development Environment Integration**: Unify access to multiple local services through one port
191
+ - **Static Resource Management**: Flexible static file serving and routing
192
+ - **Cross-origin Request Handling**: Solve CORS issues in development
193
+ - **Multi-service Integration**: Combine different backend services and frontend applications
194
+
195
+ ## Command Line Options
196
+
197
+ ```bash
198
+ agent-publish-server [options]
199
+
200
+ Options:
201
+ -c, --config <file> Configuration file path
202
+ -p, --port <port> Server port
203
+ -d, --dir <directory> Static file directory
204
+ -h, --help Display help information
205
+ -V, --version Display version number
206
+ ```
207
+
208
+ ## API
209
+
210
+ ### Programmatic Usage
211
+
212
+ ```javascript
213
+ const { createServer, startServer } = require("agent-publish-server");
214
+
215
+ const config = {
216
+ port: 3000,
217
+ dir: "./dist",
218
+ proxy: {
219
+ "/api": {
220
+ target: "http://localhost:8080",
221
+ changeOrigin: true,
222
+ },
223
+ },
224
+ };
225
+
226
+ const app = createServer(config);
227
+ startServer(app, config.port);
228
+ ```
229
+
230
+ ## Version History
231
+
232
+ - **v1.0.22**: Optimized and improved staticProxy functionality, enhanced stability
233
+ - **v1.0.18**: Enhanced staticProxy functionality, supporting both static file proxy and HTTP service proxy modes
234
+ - **v1.0.17**: Added static web proxy functionality (staticProxy), supports simultaneous use with API proxy
235
+ - **v1.0.16**: Added interactive configuration, access logging, and path validation features
236
+ - **v1.0.15**: Basic proxy and static file serving functionality
237
+
238
+ ## License
239
+
240
+ MIT © [qghs](https://github.com/qg-hs)
241
+
242
+ ## Contributing
243
+
244
+ Welcome to submit Issues and Pull Requests!
245
+
246
+ ## Support
247
+
248
+ If you encounter any problems during use, please submit an Issue on [GitHub](https://github.com/qg-hs/agent-publish-server/issues).
package/dist/server.js CHANGED
@@ -38,14 +38,27 @@ function createServer(config) {
38
38
  }
39
39
  // 配置静态网页代理
40
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}`]: '' // 移除代理路径前缀
41
+ Object.entries(config.staticProxy).forEach(([proxyPath, staticProxyConfig]) => {
42
+ const proxyType = staticProxyConfig.type || 'http'; // 默认为http类型
43
+ if (proxyType === 'static') {
44
+ // 静态文件代理
45
+ const staticPath = path_1.default.resolve(process.cwd(), staticProxyConfig.target);
46
+ if (!fs_1.default.existsSync(staticPath)) {
47
+ console.warn(`Warning: Static proxy path does not exist: ${staticPath}`);
48
+ return;
47
49
  }
48
- }));
50
+ app.use(proxyPath, express_1.default.static(staticPath));
51
+ }
52
+ else {
53
+ // HTTP服务代理
54
+ app.use(proxyPath, (0, http_proxy_middleware_1.createProxyMiddleware)({
55
+ target: staticProxyConfig.target,
56
+ changeOrigin: staticProxyConfig.changeOrigin !== false, // 默认为true
57
+ pathRewrite: {
58
+ [`^${proxyPath}`]: '' // 移除代理路径前缀
59
+ }
60
+ }));
61
+ }
49
62
  });
50
63
  }
51
64
  // 配置静态文件服务
package/dist/types.d.ts CHANGED
@@ -5,6 +5,7 @@ export interface ProxyConfig {
5
5
  }
6
6
  export interface StaticProxyConfig {
7
7
  target: string;
8
+ type?: 'http' | 'static';
8
9
  changeOrigin?: boolean;
9
10
  }
10
11
  export interface AgentConfig {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "agent-publish-server",
3
- "version": "1.0.19",
4
- "description": "基于 nodejs+express+http-proxy-middleware 的前端服务启动插件",
3
+ "version": "1.0.23",
4
+ "description": "A powerful frontend development server with API proxy, static file serving, and dual-mode static proxy support. Built with Node.js + Express + http-proxy-middleware. 基于 Node.js + Express + http-proxy-middleware 的强大前端开发服务器,支持API代理、静态文件服务和双模式静态代理。",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "bin": {
@@ -17,7 +17,28 @@
17
17
  "express",
18
18
  "proxy",
19
19
  "server",
20
- "development"
20
+ "development",
21
+ "http-proxy-middleware",
22
+ "static-server",
23
+ "dev-server",
24
+ "frontend",
25
+ "nodejs",
26
+ "cli",
27
+ "web-server",
28
+ "local-server",
29
+ "api-proxy",
30
+ "static-proxy",
31
+ "cors",
32
+ "middleware",
33
+ "reverse-proxy",
34
+ "micro-frontend",
35
+ "spa",
36
+ "build-tool",
37
+ "webpack-dev-server",
38
+ "vite",
39
+ "react",
40
+ "vue",
41
+ "angular"
21
42
  ],
22
43
  "author": {
23
44
  "name": "qghs",
@@ -3,7 +3,12 @@
3
3
  "dir": "./",
4
4
  "staticProxy": {
5
5
  "/sub_page": {
6
+ "target": "./dist",
7
+ "type": "static"
8
+ },
9
+ "/app": {
6
10
  "target": "http://localhost:3000",
11
+ "type": "http",
7
12
  "changeOrigin": true
8
13
  }
9
14
  },