agent-publish-server 1.0.26 → 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
@@ -92,6 +92,7 @@ interface AgentConfig {
92
92
  port?: number; // 服务器端口(默认:8080)
93
93
  dir?: string; // 静态文件目录(默认:"./")
94
94
  redirect?: string; // 根路径重定向,如 "/app" 表示访问 / 时重定向到 /app
95
+ fallbackRedirect?: string; // 404兜底重定向,当路由匹配不上时重定向到指定路径
95
96
  log?: boolean; // 启用访问日志(默认:true)
96
97
  proxy?: {
97
98
  // API代理配置
@@ -134,6 +135,30 @@ interface AgentConfig {
134
135
 
135
136
  这样配置后,访问 `http://localhost:3000/` 会自动重定向到 `http://localhost:3000/app`。
136
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
+
137
162
  ### 静态网页代理
138
163
 
139
164
  除了 API 代理外,还支持静态网页代理功能,用于代理整个网站或应用。
@@ -248,6 +273,7 @@ agent-publish-server --log false
248
273
 
249
274
  ### 版本历史
250
275
 
276
+ - **v1.0.27**: 新增404兜底重定向配置(fallbackRedirect),当路由匹配不上时自动重定向到指定路径,避免黑屏或404错误
251
277
  - **v1.0.26**: 新增根路径重定向配置(redirect),支持访问 / 时自动跳转到指定路径
252
278
  - **v1.0.25**: 修复 staticProxy static 类型不支持 SPA History 路由刷新的问题,添加自动 fallback 到 index.html 支持
253
279
  - **v1.0.24**: 修复移动端兼容性问题,优化 HTTP 响应头设置,确保 iOS 和 Android 显示一致性
package/dist/server.js CHANGED
@@ -155,7 +155,7 @@ function createServer(config) {
155
155
  next();
156
156
  });
157
157
  // 添加SPA应用的回退路由处理
158
- // 只有当实际文件不存在时才回退到index.html
158
+ // 只有当实际文件不存在时才回退到index.html或执行fallbackRedirect
159
159
  app.use((req, res, next) => {
160
160
  // 排除API和已处理的代理请求
161
161
  if (req.path.startsWith("/api/")) {
@@ -182,8 +182,20 @@ function createServer(config) {
182
182
  if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isFile()) {
183
183
  return res.sendFile(filePath);
184
184
  }
185
- // 如果路径不存在,回退到index.html
186
- 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");
187
199
  });
188
200
  return app;
189
201
  }
package/dist/types.d.ts CHANGED
@@ -12,6 +12,7 @@ export interface AgentConfig {
12
12
  port?: number;
13
13
  dir?: string;
14
14
  redirect?: string;
15
+ fallbackRedirect?: string;
15
16
  proxy?: Record<string, ProxyConfig>;
16
17
  staticProxy?: Record<string, StaticProxyConfig>;
17
18
  log?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-publish-server",
3
- "version": "1.0.26",
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
+ }