From 266c598d765a96453d165efe7be7372526112d96 Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Tue, 22 Oct 2024 13:02:14 +0800 Subject: [PATCH] fix: cors --- src/server/setup.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/server/setup.ts b/src/server/setup.ts index ab2c6784..b2b481e2 100644 --- a/src/server/setup.ts +++ b/src/server/setup.ts @@ -14,13 +14,20 @@ export function setupSwagger(app: INestApplication): void { const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('/swagger', app, document); app.getHttpAdapter().get('/', (req, res: FastifyReply) => { - void res.redirect(302, '/swagger'); + void res.redirect('/swagger', 302); }); } export function enableCors(app: INestApplication): void { app.enableCors({ - origin: true, + origin: (requestOrigin, callback) => { + requestOrigin = requestOrigin.trim(); + if (!requestOrigin || requestOrigin === 'null') { + callback(null, '*'); + } else { + callback(null, requestOrigin); + } + }, credentials: false, methods: ['OPTIONS', 'HEAD', 'GET', 'PUT', 'POST', 'DELETE'], allowedHeaders: ['If-Match', 'If-None-Match', 'Content-Type', 'Authorization'],