From 4f8f7c587d2745987b654194fb30c05bcbd99012 Mon Sep 17 00:00:00 2001 From: Stev_Wang <304865932@qq.com> Date: Tue, 3 Nov 2026 09:36:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E5=85=81=E8=AE=B8=20localhost=20?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E6=BA=90=E8=AE=BF=E9=97=AE=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景: - 开发环境从 http://localhost:5173 打开页面时,用户与组织页报 「成员加载失败 / Failed to fetch」; - 复现确认:API 的 WEB_ORIGIN 仅含 http://127.0.0.1:5173, 浏览器以 localhost 为 Origin 的预检响应缺少 Access-Control-Allow-Origin, 请求被浏览器拦截。 改动: - config/env.ts 的 WEB_ORIGIN 默认值同时包含 127.0.0.1 与 localhost; - .env.example 同步为逗号分隔的两个开发源。 影响面: - 仅开发环境 CORS 白名单,生产仍由部署环境变量显式配置; - 其他来源仍需按 WEB_ORIGIN 显式加入。 回滚方式: - 回滚本提交并重启 API,即恢复仅允许 127.0.0.1:5173。 验证证据: - OPTIONS /api/v1/users 对两个 Origin 均返回对应 Access-Control-Allow-Origin; - 浏览器从 localhost / 127.0.0.1 发起 /me 均返回 401(网络可达,不再 Failed to fetch); - pnpm --filter @lingxi/api typecheck / test(10 files / 60 tests)通过。 --- .env.example | 2 +- apps/api/src/config/env.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 22dd975..3b8c533 100644 --- a/.env.example +++ b/.env.example @@ -35,7 +35,7 @@ AUTH_LOCK_MINUTES=15 # ---------- API ---------- PORT=3000 HOST=0.0.0.0 -WEB_ORIGIN=http://127.0.0.1:5173 +WEB_ORIGIN=http://127.0.0.1:5173,http://localhost:5173 MAX_UPLOAD_MB=500 # ---------- Worker ---------- diff --git a/apps/api/src/config/env.ts b/apps/api/src/config/env.ts index 7bb36d9..e8b22df 100644 --- a/apps/api/src/config/env.ts +++ b/apps/api/src/config/env.ts @@ -19,7 +19,7 @@ const envSchema = z.object({ (value) => value.startsWith('redis://') || value.startsWith('rediss://'), 'REDIS_URL 必须是 redis:// 连接串', ), - WEB_ORIGIN: z.string().default('http://127.0.0.1:5173'), + WEB_ORIGIN: z.string().default('http://127.0.0.1:5173,http://localhost:5173'), MAX_UPLOAD_MB: z.coerce.number().int().min(1).max(10240).default(500), FEATURE_VIDEO: z .enum(['true', 'false'])