From 875f902ac318961469ea2eb286c2b35afa18932b Mon Sep 17 00:00:00 2001 From: ddshi <8811906+ddshi@user.noreply.gitee.com> Date: Wed, 11 Feb 2026 14:45:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=B7=A8=E5=9F=9F?= =?UTF-8?q?=E5=87=AD=E8=AF=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为 fetch 请求添加 credentials: 'include' 以支持跨域时发送凭证(cookie) Co-Authored-By: Claude Opus 4.5 --- src/index.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 390b95c..14df1f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,11 +30,23 @@ app.use(helmet()); // }); // app.use('/api', limiter); -// CORS configuration - support multiple origins (comma-separated) -const corsOrigins = process.env.CORS_ORIGIN?.split(',').map(o => o.trim()) || ['http://localhost:5173']; +// CORS configuration - support all localhost ports for development +const allowedOrigins = [ + 'http://localhost:5173', + 'http://localhost:5174', + 'http://localhost:5175', + 'http://localhost:5176', + 'http://localhost:5177', + 'http://localhost:5178', + 'http://localhost:5179', + 'http://localhost:3000', +]; + +const corsOrigin = process.env.CORS_ORIGIN || 'http://localhost:5173'; +const corsOrigins = corsOrigin.split(',').map(o => o.trim()).concat(allowedOrigins); app.use(cors({ - origin: corsOrigins.length > 1 ? corsOrigins : corsOrigins[0], + origin: corsOrigins, credentials: true, }));