CORS helps your NestJS app control who can access its resources from other websites. It keeps your app safe by blocking unwanted requests.
CORS configuration in NestJS
const app = await NestFactory.create(AppModule); app.enableCors({ origin: 'http://example.com', methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'], credentials: true, }); await app.listen(3000);
The origin option controls which website URLs can access your backend.
Use credentials: true if you want to allow cookies or authorization headers.
app.enableCors();
https://myfrontend.com.app.enableCors({ origin: 'https://myfrontend.com' });app.enableCors({ origin: ['https://site1.com', 'https://site2.com'] });app.enableCors({
origin: '*',
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type', 'Authorization']
});This NestJS app allows CORS requests only from http://localhost:4200 with GET and POST methods. It also allows credentials like cookies.
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableCors({ origin: 'http://localhost:4200', methods: ['GET', 'POST'], credentials: true, }); await app.listen(3000); console.log('Server running on http://localhost:3000'); } bootstrap();
Be careful with origin: '*' when using credentials; browsers block this combination.
You can also configure CORS globally in the main.ts file or per route using decorators.
Use browser DevTools Network tab to check CORS headers and errors when testing.
CORS controls which websites can access your NestJS backend.
Use app.enableCors() to set CORS options simply.
Always specify allowed origins and methods to keep your app secure.
Practice
Solution
Step 1: Understand CORS purpose
CORS (Cross-Origin Resource Sharing) controls which websites can access backend resources from a different origin.Step 2: Identify correct purpose in NestJS
Enabling CORS in NestJS allows specific web origins to access the backend safely.Final Answer:
To allow web browsers from specific origins to access the backend resources -> Option DQuick Check:
CORS controls access by origin = C [OK]
- Confusing CORS with encryption
- Thinking CORS speeds up server
- Mixing CORS with database management
Solution
Step 1: Recall NestJS CORS enabling method
The official method to enable CORS isapp.enableCors()without parameters for default settings.Step 2: Check options for correctness
Options B, C, and D are not valid NestJS methods or have incorrect syntax.Final Answer:
app.enableCors() -> Option CQuick Check:
Default CORS enable = app.enableCors() [OK]
- Using incorrect method names like setCors or useCors
- Passing invalid parameters to enableCors
- Confusing CORS enabling with middleware usage
app.enableCors({ origin: 'https://example.com' });Solution
Step 1: Analyze the origin option in enableCors
The origin option is set to 'https://example.com', which restricts access to this exact origin.Step 2: Understand CORS behavior with origin setting
Only requests coming from 'https://example.com' will be allowed by CORS policy.Final Answer:
Only https://example.com can access the backend -> Option AQuick Check:
origin restricts access = B [OK]
- Assuming origin '*' is default
- Thinking multiple origins allowed without array
- Confusing origin with methods or headers
app.enableCors({ origins: ['https://site1.com', 'https://site2.com'] });Solution
Step 1: Check property name for allowed origins
The correct property name is 'origin' (singular), not 'origins'.Step 2: Confirm array usage for multiple origins
Passing an array to 'origin' is valid to allow multiple origins.Final Answer:
The property 'origins' is incorrect; it should be 'origin' -> Option BQuick Check:
Correct property name is origin = A [OK]
- Using 'origins' instead of 'origin'
- Thinking enableCors rejects options
- Removing protocol from URLs
Solution
Step 1: Verify property names and values
The property for allowed origins is 'origin' (singular), and it accepts an array for multiple domains. The 'methods' property accepts an array of HTTP methods.Step 2: Check each option for correctness
app.enableCors({ origin: ['https://app1.com', 'https://app2.com'], methods: ['GET', 'POST'] }); uses correct property names and value types. app.enableCors({ origins: ['https://app1.com', 'https://app2.com'], methods: 'GET,POST' }); uses 'origins' (incorrect) and methods as a string (less preferred). app.enableCors({ origin: '*', methods: ['GET', 'POST'] }); allows all origins ('*'), which is not desired. app.enableCors({ origin: ['https://app1.com', 'https://app2.com'], method: ['GET', 'POST'] }); uses 'method' (singular), which is incorrect.Final Answer:
app.enableCors({ origin: ['https://app1.com', 'https://app2.com'], methods: ['GET', 'POST'] }); -> Option AQuick Check:
Correct keys and array values = D [OK]
- Using 'origins' instead of 'origin'
- Passing methods as comma string instead of array
- Using 'method' instead of 'methods'
- Allowing all origins with '*' when not intended
Ready to prove it?
Pass the test and this pattern moves unknown
