agent-publish-server 1.0.15 → 1.0.17

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,80 +1,359 @@
1
- ## agent-publish-server
1
+ # agent-publish-server
2
2
 
3
- > 这是一个基于 nodejs+express+http-proxy-middleware 的一款可支持代理的前端服务启动插件
3
+ [English](#english) | [中文](#chinese)
4
+
5
+ ---
6
+
7
+ ## <a id="english"></a>English
8
+
9
+ > A frontend service startup plugin based on nodejs+express+http-proxy-middleware with proxy support
4
10
 
5
- ## 安装
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
6
22
 
7
23
  ```bash
8
24
  npm install agent-publish-server -g
9
25
  ```
10
26
 
11
- ## 使用步骤
27
+ ### Quick Start
12
28
 
13
- ### 步骤 1:初始化配置文件
14
-
15
- 首先,初始化配置文件:
29
+ #### Method 1: Interactive Configuration (Recommended)
16
30
 
17
31
  ```bash
18
- agent-publish-server init
32
+ agent-publish-server -wp
19
33
  ```
20
34
 
21
- 这将在当前目录生成默认配置文件 `agent_config.json`。
35
+ This will guide you through an interactive setup:
22
36
 
23
- ### 步骤 2:自定义配置(可选)
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
24
42
 
25
- 根据需要修改生成的 `agent_config.json` 配置文件。配置格式如下:
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`:**
26
52
 
27
53
  ```json
28
54
  {
29
- "port": 8080, // 端口号
30
- "dir": "./", // 静态文件目录
55
+ "port": 8080,
56
+ "dir": "./",
57
+ "log": true,
31
58
  "proxy": {
32
59
  "/api": {
33
- // 代理路径
34
- "target": "URL_ADDRESS", // 代理地址
35
- "changeOrigin": true, // 是否改变域名
60
+ "target": "http://localhost:3000",
61
+ "changeOrigin": true,
36
62
  "pathRewrite": {
37
- // 路径重写
38
- "^/api": "" // 重写后路径
63
+ "^/api": ""
39
64
  }
40
65
  }
66
+ },
67
+ "staticProxy": {
68
+ "/sub_page": {
69
+ "target": "http://localhost:3000",
70
+ "changeOrigin": true
71
+ }
41
72
  }
42
73
  }
43
74
  ```
44
75
 
45
- ### 步骤 3:启动服务
76
+ 3. **Start the server:**
46
77
 
47
78
  ```bash
48
- agent-publish-server
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"
49
128
  ```
50
129
 
51
- ## 配置选项
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 支持**: 单页应用路由支持
164
+
165
+ ### 安装
166
+
167
+ ```bash
168
+ npm install agent-publish-server -g
169
+ ```
170
+
171
+ ### 快速开始
172
+
173
+ #### 方式一:交互式配置(推荐)
174
+
175
+ ```bash
176
+ agent-publish-server -wp
177
+ ```
178
+
179
+ 这将引导您完成交互式设置:
180
+
181
+ 1. 输入端口号
182
+ 2. 输入项目地址(打包后)
183
+ 3. 输入代理接口配置(格式:`/api:http://localhost:3000`)
184
+ 4. 选择是否继续添加代理(1:继续,0:结束)
185
+ 5. 生成 `agent_config.json` 并获得启动命令提示
186
+
187
+ #### 方式二:手动配置
188
+
189
+ 1. **初始化配置文件:**
190
+
191
+ ```bash
192
+ agent-publish-server init
193
+ ```
52
194
 
53
- ### 指定配置文件
195
+ 2. **编辑生成的 `agent_config.json`:**
54
196
 
55
- 你可以通过以下方式指定自定义配置文件路径:
197
+ ```json
198
+ {
199
+ "port": 8080,
200
+ "dir": "./",
201
+ "log": true,
202
+ "proxy": {
203
+ "/api": {
204
+ "target": "http://localhost:3000",
205
+ "changeOrigin": true,
206
+ "pathRewrite": {
207
+ "^/api": ""
208
+ }
209
+ }
210
+ }
211
+ }
212
+ ```
56
213
 
57
- 1. 使用相对路径(相对于当前工作目录):
214
+ 3. **启动服务:**
58
215
 
59
216
  ```bash
60
217
  agent-publish-server -c ./agent_config.json
61
218
  ```
62
219
 
63
- 2. 使用绝对路径:
220
+ ### 命令行选项
221
+
222
+ | 选项 | 说明 | 示例 |
223
+ | --------------------- | ----------------- | --------------------------------------- |
224
+ | `-wp, --write-proxy` | 交互式代理配置 | `agent-publish-server -wp` |
225
+ | `-c, --config <路径>` | 指定配置文件路径 | `agent-publish-server -c ./config.json` |
226
+ | `-p, --port <端口>` | 覆盖端口号 | `agent-publish-server -p 3000` |
227
+ | `-d, --dir <目录>` | 覆盖静态文件目录 | `agent-publish-server -d ./dist` |
228
+ | `--log <布尔值>` | 启用/禁用访问日志 | `agent-publish-server --log false` |
229
+ | `-v, --version` | 显示版本 | `agent-publish-server -v` |
230
+ | `init` | 初始化配置文件 | `agent-publish-server init` |
231
+
232
+ ### 配置文件格式
233
+
234
+ ```typescript
235
+ interface AgentConfig {
236
+ port?: number; // 服务器端口(默认:8080)
237
+ dir?: string; // 静态文件目录(默认:"./")
238
+ log?: boolean; // 启用访问日志(默认:true)
239
+ proxy?: {
240
+ // API代理配置
241
+ [path: string]: {
242
+ target: string; // 目标URL
243
+ changeOrigin?: boolean; // 更改源头
244
+ pathRewrite?: {
245
+ // 路径重写规则
246
+ [pattern: string]: string;
247
+ };
248
+ };
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
+ }
274
+ }
275
+ ```
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
+
311
+ ### 访问日志
312
+
313
+ 访问日志默认启用,显示内容包括:
314
+
315
+ - 时间戳(ISO 格式)
316
+ - 客户端 IP 地址
317
+ - HTTP 方法和 URL
318
+ - 用户代理字符串
319
+
320
+ 日志输出示例:
321
+
322
+ ```
323
+ [2025-08-27T11:02:48.854Z] ::1 "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
324
+ ```
325
+
326
+ 禁用日志:
64
327
 
65
328
  ```bash
66
- agent-publish-server -c /path/to/agent_config.json
329
+ agent-publish-server --log false
67
330
  ```
68
331
 
69
- 支持的配置文件参数:
332
+ ### 错误处理
333
+
334
+ - **项目路径无效**:显示"项目路径错误"并退出
335
+ - **端口号无效**:验证端口号范围(1-65535)
336
+ - **配置缺失**:使用默认配置
337
+
338
+ ### 使用场景
339
+
340
+ - **React/Vue 开发**:为构建后的应用提供服务并代理 API
341
+ - **前端测试**:快速静态文件服务,处理跨域问题
342
+ - **本地开发**:代理 API 调用到后端服务
343
+ - **SPA 部署**:为单页应用提供正确的路由支持
344
+ - **微前端架构**:通过静态代理整合多个前端应用
345
+ - **开发环境整合**:将多个本地服务统一到一个端口访问
346
+
347
+ ### 版本历史
348
+
349
+ - **v1.0.17**: 新增静态网页代理功能(staticProxy),支持与API代理同时使用
350
+ - **v1.0.16**: 新增交互式配置、访问日志、路径验证功能
351
+ - **v1.0.15**: 基础代理和静态文件服务功能
352
+
353
+ ### 许可证
70
354
 
71
- - `-c`
72
- - `--config`
73
- - `--cf`
74
- - `--config-file`
355
+ MIT License
75
356
 
76
- ## 其他说明
357
+ ### 贡献
77
358
 
78
- - 适配 React、Vue 等前端框架
79
- - 使用 `-v` 或 `--version` 查看当前版本
80
- - 默认配置文件名为 `agent_config.json`
359
+ 欢迎提交 Issue Pull Request!
package/dist/cli.js CHANGED
@@ -1,11 +1,116 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
3
36
  Object.defineProperty(exports, "__esModule", { value: true });
4
37
  const commander_1 = require("commander");
5
38
  const config_1 = require("./config");
6
39
  const server_1 = require("./server");
7
40
  const fs_1 = require("fs");
8
41
  const path_1 = require("path");
42
+ const readline = __importStar(require("readline"));
43
+ // 交互式配置函数
44
+ async function interactiveWriteProxyConfig() {
45
+ const rl = readline.createInterface({
46
+ input: process.stdin,
47
+ output: process.stdout
48
+ });
49
+ const question = (prompt) => {
50
+ return new Promise((resolve) => {
51
+ rl.question(prompt, resolve);
52
+ });
53
+ };
54
+ try {
55
+ // 询问端口号
56
+ const portInput = await question('请输入端口号: ');
57
+ const port = parseInt(portInput.trim(), 10);
58
+ if (isNaN(port) || port <= 0 || port > 65535) {
59
+ throw new Error('无效的端口号,请输入1-65535之间的数字');
60
+ }
61
+ // 询问项目地址
62
+ const dirInput = await question('请输入项目地址(打包后): ');
63
+ const dir = dirInput.trim() || './';
64
+ const config = {
65
+ port,
66
+ dir,
67
+ proxy: {},
68
+ log: true // 默认启用日志
69
+ };
70
+ // 循环添加代理配置
71
+ while (true) {
72
+ const proxyInput = await question('请输入代理接口(格式例如:/api:http://localhost:3000): ');
73
+ const proxyStr = proxyInput.trim();
74
+ if (proxyStr) {
75
+ const parts = proxyStr.split(':');
76
+ if (parts.length < 2) {
77
+ console.log('格式错误,请按照 /api:http://localhost:3000 的格式输入');
78
+ continue;
79
+ }
80
+ const path = parts[0].trim();
81
+ const target = parts.slice(1).join(':'); // 处理URL中的冒号
82
+ if (!path.startsWith('/')) {
83
+ console.log('代理路径必须以 / 开头');
84
+ continue;
85
+ }
86
+ if (!target.startsWith('http://') && !target.startsWith('https://')) {
87
+ console.log('代理目标地址必须是完整的URL(以http://或https://开头)');
88
+ continue;
89
+ }
90
+ config.proxy[path] = {
91
+ target: target,
92
+ changeOrigin: true,
93
+ pathRewrite: {
94
+ [`^${path}`]: ''
95
+ }
96
+ };
97
+ console.log(`✓ 已添加代理:${path} -> ${target}`);
98
+ }
99
+ const continueInput = await question('是否继续添加代理接口:1:继续,0:结束: ');
100
+ if (continueInput.trim() === '0') {
101
+ break;
102
+ }
103
+ else if (continueInput.trim() !== '1') {
104
+ console.log('请输入 1 继续或 0 结束');
105
+ continue;
106
+ }
107
+ }
108
+ return config;
109
+ }
110
+ finally {
111
+ rl.close();
112
+ }
113
+ }
9
114
  const program = new commander_1.Command();
10
115
  const packageJson = require('../package.json');
11
116
  program
@@ -14,11 +119,30 @@ program
14
119
  .option('--cf <path>', '配置文件路径')
15
120
  .option('--config-file <path>', '配置文件路径')
16
121
  .option('-p, --port <number>', '服务器端口号')
17
- .option('-d, --dir <path>', '静态文件目录路径');
122
+ .option('-d, --dir <path>', '静态文件目录路径')
123
+ .option('-wp, --write-proxy', '交互式配置代理服务')
124
+ .option('--log <boolean>', '是否显示访问日志 (true/false),默认为true');
18
125
  // 添加一个默认命令,用于处理没有指定任何子命令的情况
19
126
  program
20
- .action(() => {
127
+ .action(async () => {
21
128
  const options = program.opts();
129
+ // 检查是否使用 -wp 选项
130
+ if (options.writeProxy) {
131
+ try {
132
+ console.log('✨ 欢迎使用交互式代理配置\n');
133
+ // 交互式配置
134
+ const config = await interactiveWriteProxyConfig();
135
+ const configPath = (0, path_1.join)(process.cwd(), 'agent_config.json');
136
+ (0, fs_1.writeFileSync)(configPath, JSON.stringify(config, null, 2));
137
+ console.log('\n✓ 已配置完成,请运行:agent-publish-server -c ./agent_config.json');
138
+ process.exit(0);
139
+ }
140
+ catch (error) {
141
+ const errorMessage = error instanceof Error ? error.message : String(error);
142
+ console.error('\n✗ 配置失败:', errorMessage);
143
+ process.exit(1);
144
+ }
145
+ }
22
146
  // 加载配置
23
147
  const config = (0, config_1.loadConfig)(options);
24
148
  // 命令行参数优先级高于配置文件
@@ -33,6 +157,20 @@ program
33
157
  if (options.dir) {
34
158
  config.dir = options.dir;
35
159
  }
160
+ // 处理日志参数
161
+ if (options.log !== undefined) {
162
+ const logValue = options.log.toLowerCase();
163
+ if (logValue === 'true') {
164
+ config.log = true;
165
+ }
166
+ else if (logValue === 'false') {
167
+ config.log = false;
168
+ }
169
+ else {
170
+ console.error('日志参数必须是 true 或 false');
171
+ process.exit(1);
172
+ }
173
+ }
36
174
  // 启动服务器
37
175
  (0, server_1.startServer)(config).catch((error) => {
38
176
  console.error('Server failed to start:', error);
package/dist/server.js CHANGED
@@ -12,12 +12,42 @@ const fs_1 = __importDefault(require("fs"));
12
12
  function createServer(config) {
13
13
  const app = (0, express_1.default)();
14
14
  const staticDir = config.dir || './';
15
- // 配置代理
15
+ const enableLog = config.log !== false; // 默认为true
16
+ // 检查项目路径是否存在
17
+ const absoluteStaticDir = path_1.default.resolve(process.cwd(), staticDir);
18
+ if (!fs_1.default.existsSync(absoluteStaticDir)) {
19
+ throw new Error('项目路径错误');
20
+ }
21
+ // 添加日志中间件
22
+ if (enableLog) {
23
+ app.use((req, res, next) => {
24
+ const timestamp = new Date().toISOString();
25
+ const method = req.method;
26
+ const url = req.url;
27
+ const userAgent = req.get('User-Agent') || '-';
28
+ const ip = req.ip || req.connection.remoteAddress || '-';
29
+ console.log(`[${timestamp}] ${ip} "${method} ${url}" "${userAgent}"`);
30
+ next();
31
+ });
32
+ }
33
+ // 配置API代理
16
34
  if (config.proxy) {
17
35
  Object.entries(config.proxy).forEach(([path, proxyConfig]) => {
18
36
  app.use(path, (0, http_proxy_middleware_1.createProxyMiddleware)(proxyConfig));
19
37
  });
20
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
+ }
21
51
  // 配置静态文件服务
22
52
  app.use(express_1.default.static(staticDir));
23
53
  // 添加SPA应用的回退路由处理
@@ -27,6 +57,14 @@ function createServer(config) {
27
57
  if (req.path.startsWith('/api/')) {
28
58
  return next();
29
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
+ }
30
68
  const filePath = path_1.default.join(process.cwd(), staticDir, req.path);
31
69
  // 检查是否为目录
32
70
  if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isDirectory()) {
@@ -46,18 +84,25 @@ function createServer(config) {
46
84
  return app;
47
85
  }
48
86
  function startServer(config) {
49
- const app = createServer(config);
50
- const port = config.port || 8080;
51
- return new Promise((resolve, reject) => {
52
- try {
53
- app.listen(port, () => {
54
- console.log(`Server is running at http://localhost:${port}`);
55
- resolve();
56
- });
57
- }
58
- catch (error) {
59
- console.error('Failed to start server:', error);
60
- reject(error);
61
- }
62
- });
87
+ try {
88
+ const app = createServer(config);
89
+ const port = config.port || 8080;
90
+ return new Promise((resolve, reject) => {
91
+ try {
92
+ app.listen(port, () => {
93
+ console.log(`Server is running at http://localhost:${port}`);
94
+ resolve();
95
+ });
96
+ }
97
+ catch (error) {
98
+ console.error('Failed to start server:', error);
99
+ reject(error);
100
+ }
101
+ });
102
+ }
103
+ catch (error) {
104
+ const errorMessage = error instanceof Error ? error.message : String(error);
105
+ console.error(errorMessage);
106
+ process.exit(1);
107
+ }
63
108
  }
package/dist/types.d.ts CHANGED
@@ -3,10 +3,16 @@ 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>;
15
+ log?: boolean;
10
16
  }
11
17
  export interface CommandOptions {
12
18
  config?: string;
@@ -15,4 +21,6 @@ export interface CommandOptions {
15
21
  configFile?: string;
16
22
  port?: string;
17
23
  dir?: string;
24
+ writeProxy?: boolean;
25
+ log?: string;
18
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-publish-server",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "基于 nodejs+express+http-proxy-middleware 的前端服务启动插件",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,11 @@
1
+ {
2
+ "port": 8080,
3
+ "dir": "./",
4
+ "staticProxy": {
5
+ "/fy_user_point": {
6
+ "target": "http://localhost:3000",
7
+ "changeOrigin": true
8
+ }
9
+ },
10
+ "log": true
11
+ }