Discover how a simple setting can unlock your app's ability to talk to the whole web safely!
Why CORS configuration in NestJS? - Purpose & Use Cases
Imagine you build a website that needs to get data from another server, but when you try to fetch it, the browser blocks your request without a clear reason.
Manually handling cross-origin requests is tricky because browsers block requests from different domains by default for security. Without proper setup, your app can't talk to other servers, causing confusing errors and broken features.
CORS configuration in NestJS lets you easily tell the browser which external sites are allowed to access your server, making cross-site communication safe and smooth.
app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); next(); });app.enableCors({ origin: 'https://example.com' });It enables your server to safely share data with trusted websites, unlocking powerful integrations and richer user experiences.
Think of a weather app on your phone fetching live data from a remote weather service; CORS configuration ensures your app can get that data without being blocked.
CORS prevents unauthorized cross-site requests by default.
Manual fixes are error-prone and insecure.
NestJS CORS config makes safe cross-origin sharing easy.
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
