agent-publish-server 1.0.22 → 1.0.24

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
 
@@ -372,9 +228,11 @@ agent-publish-server --log false
372
228
 
373
229
  ### 版本历史
374
230
 
375
- - **v1.0.22**: 优化和完善staticProxy功能,提升稳定性
376
- - **v1.0.18**: 增强staticProxy功能,支持静态文件代理和HTTP服务代理两种模式
377
- - **v1.0.17**: 新增静态网页代理功能(staticProxy),支持与API代理同时使用
231
+ - **v1.0.24**: 修复移动端兼容性问题,优化HTTP响应头设置,确保iOS和Android显示一致性
232
+ - **v1.0.23**: 完善双语文档支持,优化package.json关键词,提升npm包曝光度
233
+ - **v1.0.22**: 优化和完善 staticProxy 功能,提升稳定性
234
+ - **v1.0.18**: 增强 staticProxy 功能,支持静态文件代理和 HTTP 服务代理两种模式
235
+ - **v1.0.17**: 新增静态网页代理功能(staticProxy),支持与 API 代理同时使用
378
236
  - **v1.0.16**: 新增交互式配置、访问日志、路径验证功能
379
237
  - **v1.0.15**: 基础代理和静态文件服务功能
380
238
 
package/README_EN.md ADDED
@@ -0,0 +1,250 @@
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.24**: Fixed mobile compatibility issues, optimized HTTP response headers for consistent iOS and Android display
233
+ - **v1.0.23**: Enhanced bilingual documentation support, optimized package.json keywords for better npm exposure
234
+ - **v1.0.22**: Optimized and improved staticProxy functionality, enhanced stability
235
+ - **v1.0.18**: Enhanced staticProxy functionality, supporting both static file proxy and HTTP service proxy modes
236
+ - **v1.0.17**: Added static web proxy functionality (staticProxy), supports simultaneous use with API proxy
237
+ - **v1.0.16**: Added interactive configuration, access logging, and path validation features
238
+ - **v1.0.15**: Basic proxy and static file serving functionality
239
+
240
+ ## License
241
+
242
+ MIT © [qghs](https://github.com/qg-hs)
243
+
244
+ ## Contributing
245
+
246
+ Welcome to submit Issues and Pull Requests!
247
+
248
+ ## Support
249
+
250
+ 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
@@ -47,7 +47,33 @@ function createServer(config) {
47
47
  console.warn(`Warning: Static proxy path does not exist: ${staticPath}`);
48
48
  return;
49
49
  }
50
- app.use(proxyPath, express_1.default.static(staticPath));
50
+ app.use(proxyPath, express_1.default.static(staticPath, {
51
+ setHeaders: (res, filePath, stat) => {
52
+ // 设置通用响应头,提升移动端兼容性
53
+ res.setHeader('X-Content-Type-Options', 'nosniff');
54
+ res.setHeader('X-Frame-Options', 'SAMEORIGIN');
55
+ res.setHeader('X-XSS-Protection', '1; mode=block');
56
+ // 针对HTML文件设置移动端优化响应头
57
+ if (filePath.endsWith('.html')) {
58
+ res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
59
+ res.setHeader('Pragma', 'no-cache');
60
+ res.setHeader('Expires', '0');
61
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
62
+ }
63
+ // 针对CSS文件确保正确的Content-Type
64
+ if (filePath.endsWith('.css')) {
65
+ res.setHeader('Content-Type', 'text/css; charset=utf-8');
66
+ }
67
+ // 针对JS文件确保正确的Content-Type
68
+ if (filePath.endsWith('.js')) {
69
+ res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
70
+ }
71
+ // 针对图片文件设置适当的缓存
72
+ if (filePath.match(/\.(jpg|jpeg|png|gif|webp|svg)$/i)) {
73
+ res.setHeader('Cache-Control', 'public, max-age=31536000');
74
+ }
75
+ }
76
+ }));
51
77
  }
52
78
  else {
53
79
  // HTTP服务代理
@@ -61,8 +87,46 @@ function createServer(config) {
61
87
  }
62
88
  });
63
89
  }
64
- // 配置静态文件服务
65
- app.use(express_1.default.static(staticDir));
90
+ // 配置静态文件服务,添加移动端兼容性响应头
91
+ app.use(express_1.default.static(staticDir, {
92
+ setHeaders: (res, filePath, stat) => {
93
+ // 设置通用响应头,提升移动端兼容性
94
+ res.setHeader('X-Content-Type-Options', 'nosniff');
95
+ res.setHeader('X-Frame-Options', 'SAMEORIGIN');
96
+ res.setHeader('X-XSS-Protection', '1; mode=block');
97
+ // 针对HTML文件设置移动端优化响应头
98
+ if (filePath.endsWith('.html')) {
99
+ res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
100
+ res.setHeader('Pragma', 'no-cache');
101
+ res.setHeader('Expires', '0');
102
+ // 确保正确的Content-Type
103
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
104
+ }
105
+ // 针对CSS文件确保正确的Content-Type
106
+ if (filePath.endsWith('.css')) {
107
+ res.setHeader('Content-Type', 'text/css; charset=utf-8');
108
+ }
109
+ // 针对JS文件确保正确的Content-Type
110
+ if (filePath.endsWith('.js')) {
111
+ res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
112
+ }
113
+ // 针对图片文件设置适当的缓存
114
+ if (filePath.match(/\.(jpg|jpeg|png|gif|webp|svg)$/i)) {
115
+ res.setHeader('Cache-Control', 'public, max-age=31536000');
116
+ }
117
+ }
118
+ }));
119
+ // 添加移动端兼容性中间件
120
+ app.use((req, res, next) => {
121
+ // 检测移动端User-Agent并设置相应响应头
122
+ const userAgent = req.get('User-Agent') || '';
123
+ const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent);
124
+ if (isMobile) {
125
+ // 为移动端设置额外的响应头
126
+ res.setHeader('Vary', 'User-Agent');
127
+ }
128
+ next();
129
+ });
66
130
  // 添加SPA应用的回退路由处理
67
131
  // 只有当实际文件不存在时才回退到index.html
68
132
  app.use((req, res, next) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "agent-publish-server",
3
- "version": "1.0.22",
4
- "description": "基于 nodejs+express+http-proxy-middleware 的前端服务启动插件",
3
+ "version": "1.0.24",
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",