agent-publish-server 1.0.25 → 1.0.27

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
@@ -91,6 +91,8 @@ agent-publish-server -c ./agent_config.json
91
91
  interface AgentConfig {
92
92
  port?: number; // 服务器端口(默认:8080)
93
93
  dir?: string; // 静态文件目录(默认:"./")
94
+ redirect?: string; // 根路径重定向,如 "/app" 表示访问 / 时重定向到 /app
95
+ fallbackRedirect?: string; // 404兜底重定向,当路由匹配不上时重定向到指定路径
94
96
  log?: boolean; // 启用访问日志(默认:true)
95
97
  proxy?: {
96
98
  // API代理配置
@@ -114,6 +116,49 @@ interface AgentConfig {
114
116
  }
115
117
  ```
116
118
 
119
+ ### 根路径重定向
120
+
121
+ 通过 `redirect` 配置实现访问根路径时自动重定向到指定路径:
122
+
123
+ ```json
124
+ {
125
+ "port": 3000,
126
+ "redirect": "/app",
127
+ "staticProxy": {
128
+ "/app": {
129
+ "target": "./dist",
130
+ "type": "static"
131
+ }
132
+ }
133
+ }
134
+ ```
135
+
136
+ 这样配置后,访问 `http://localhost:3000/` 会自动重定向到 `http://localhost:3000/app`。
137
+
138
+ ### 404兜底重定向
139
+
140
+ 通过 `fallbackRedirect` 配置实现当路由匹配不上时自动重定向到指定路径,避免出现黑屏或404页面:
141
+
142
+ ```json
143
+ {
144
+ "port": 3000,
145
+ "redirect": "/app",
146
+ "fallbackRedirect": "/app",
147
+ "staticProxy": {
148
+ "/app": {
149
+ "target": "./dist",
150
+ "type": "static"
151
+ }
152
+ }
153
+ }
154
+ ```
155
+
156
+ 这样配置后:
157
+
158
+ - 访问 `http://localhost:3000/` 会自动重定向到 `/app`
159
+ - 访问不存在的路由(如 `/unknown`)也会重定向到 `/app`
160
+ - 已配置的 `staticProxy` 路径仍正常访问,不受影响
161
+
117
162
  ### 静态网页代理
118
163
 
119
164
  除了 API 代理外,还支持静态网页代理功能,用于代理整个网站或应用。
@@ -228,6 +273,8 @@ agent-publish-server --log false
228
273
 
229
274
  ### 版本历史
230
275
 
276
+ - **v1.0.27**: 新增404兜底重定向配置(fallbackRedirect),当路由匹配不上时自动重定向到指定路径,避免黑屏或404错误
277
+ - **v1.0.26**: 新增根路径重定向配置(redirect),支持访问 / 时自动跳转到指定路径
231
278
  - **v1.0.25**: 修复 staticProxy static 类型不支持 SPA History 路由刷新的问题,添加自动 fallback 到 index.html 支持
232
279
  - **v1.0.24**: 修复移动端兼容性问题,优化 HTTP 响应头设置,确保 iOS 和 Android 显示一致性
233
280
  - **v1.0.23**: 完善双语文档支持,优化 package.json 关键词,提升 npm 包曝光度
package/README_EN.md CHANGED
@@ -77,9 +77,29 @@ agent-publish-server -c config.json
77
77
 
78
78
  - **port**: Server port (default: 3000)
79
79
  - **dir**: Static file directory (default: current directory)
80
+ - **redirect**: Root path redirect, e.g. "/app" redirects / to /app
80
81
  - **proxy**: API proxy configuration
81
82
  - **staticProxy**: Static proxy configuration
82
83
 
84
+ ### Root Path Redirect
85
+
86
+ Use `redirect` to automatically redirect root path to a specified path:
87
+
88
+ ```json
89
+ {
90
+ "port": 3000,
91
+ "redirect": "/app",
92
+ "staticProxy": {
93
+ "/app": {
94
+ "target": "./dist",
95
+ "type": "static"
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ With this config, accessing `http://localhost:3000/` will redirect to `http://localhost:3000/app`.
102
+
83
103
  #### API Proxy Configuration (ProxyConfig)
84
104
 
85
105
  ```typescript
@@ -229,6 +249,7 @@ startServer(app, config.port);
229
249
 
230
250
  ## Version History
231
251
 
252
+ - **v1.0.26**: Added root path redirect configuration (redirect), supports automatic redirect when accessing /
232
253
  - **v1.0.25**: Fixed staticProxy static type not supporting SPA History route refresh, added automatic fallback to index.html
233
254
  - **v1.0.24**: Fixed mobile compatibility issues, optimized HTTP response headers for consistent iOS and Android display
234
255
  - **v1.0.23**: Enhanced bilingual documentation support, optimized package.json keywords for better npm exposure
package/dist/server.js CHANGED
@@ -30,6 +30,15 @@ function createServer(config) {
30
30
  next();
31
31
  });
32
32
  }
33
+ // 根路径重定向配置
34
+ if (config.redirect) {
35
+ app.get("/", (req, res) => {
36
+ const redirectPath = config.redirect.startsWith("/")
37
+ ? config.redirect
38
+ : `/${config.redirect}`;
39
+ res.redirect(302, redirectPath);
40
+ });
41
+ }
33
42
  // 配置API代理
34
43
  if (config.proxy) {
35
44
  Object.entries(config.proxy).forEach(([path, proxyConfig]) => {
@@ -146,7 +155,7 @@ function createServer(config) {
146
155
  next();
147
156
  });
148
157
  // 添加SPA应用的回退路由处理
149
- // 只有当实际文件不存在时才回退到index.html
158
+ // 只有当实际文件不存在时才回退到index.html或执行fallbackRedirect
150
159
  app.use((req, res, next) => {
151
160
  // 排除API和已处理的代理请求
152
161
  if (req.path.startsWith("/api/")) {
@@ -173,8 +182,20 @@ function createServer(config) {
173
182
  if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isFile()) {
174
183
  return res.sendFile(filePath);
175
184
  }
176
- // 如果路径不存在,回退到index.html
177
- res.sendFile(path_1.default.join(process.cwd(), staticDir, "index.html"));
185
+ // 如果配置了fallbackRedirect,则重定向到指定路径
186
+ if (config.fallbackRedirect) {
187
+ const redirectPath = config.fallbackRedirect.startsWith("/")
188
+ ? config.fallbackRedirect
189
+ : `/${config.fallbackRedirect}`;
190
+ return res.redirect(302, redirectPath);
191
+ }
192
+ // 如果路径不存在且未配置fallbackRedirect,回退到index.html
193
+ const rootIndexPath = path_1.default.join(process.cwd(), staticDir, "index.html");
194
+ if (fs_1.default.existsSync(rootIndexPath)) {
195
+ return res.sendFile(rootIndexPath);
196
+ }
197
+ // 如果index.html也不存在,返回404
198
+ res.status(404).send("404 Not Found");
178
199
  });
179
200
  return app;
180
201
  }
package/dist/types.d.ts CHANGED
@@ -5,12 +5,14 @@ export interface ProxyConfig {
5
5
  }
6
6
  export interface StaticProxyConfig {
7
7
  target: string;
8
- type?: 'http' | 'static';
8
+ type?: "http" | "static";
9
9
  changeOrigin?: boolean;
10
10
  }
11
11
  export interface AgentConfig {
12
12
  port?: number;
13
13
  dir?: string;
14
+ redirect?: string;
15
+ fallbackRedirect?: string;
14
16
  proxy?: Record<string, ProxyConfig>;
15
17
  staticProxy?: Record<string, StaticProxyConfig>;
16
18
  log?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-publish-server",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
4
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",
@@ -0,0 +1,18 @@
1
+ {
2
+ "port": 8080,
3
+ "dir": "./",
4
+ "redirect": "/sub_page",
5
+ "fallbackRedirect": "/sub_page",
6
+ "staticProxy": {
7
+ "/sub_page": {
8
+ "target": "./dist",
9
+ "type": "static"
10
+ },
11
+ "/app": {
12
+ "target": "http://localhost:3000",
13
+ "type": "http",
14
+ "changeOrigin": true
15
+ }
16
+ },
17
+ "log": true
18
+ }