Skip to content

Commit eebeda3

Browse files
committed
fix: fix cors issue when NextJS call api
1 parent 3d3b88f commit eebeda3

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
WEB_APP_URL=
12
# SUPABASE
23
SUPABASE_URL=
34
SUPABASE_ANON_KEY=

src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ import { apiReference } from '@scalar/nestjs-api-reference';
1717
validateEnv();
1818

1919
async function bootstrap() {
20+
const adapter = new FastifyAdapter();
21+
adapter.enableCors({
22+
origin: [process.env.WEB_APP_URL || 'http://localhost:3000'],
23+
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
24+
});
2025
const app = await NestFactory.create<NestFastifyApplication>(
2126
AppModule,
22-
new FastifyAdapter(),
27+
adapter,
2328
);
2429

2530
app.enableVersioning({

src/utils/validateEnv.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ function validateEnv() {
66
PORT: port({ default: 3000 }),
77
HOST: str({ default: '0.0.0.0' }),
88

9+
// Web App
10+
WEB_APP_URL: url({ default: 'http://localhost:3000' }),
11+
912
// Typesense
1013
TYPESENSE_HOST: str(),
1114
TYPESENSE_PORT: port(),

0 commit comments

Comments
 (0)