📖 Scenario: You are building a simple NestJS backend server that will be accessed by a frontend running on a different domain. To allow the frontend to communicate with your backend, you need to configure CORS (Cross-Origin Resource Sharing) properly.
🎯 Goal: Configure CORS in a NestJS application to allow requests only from http://example.com and enable credentials support.
📋 What You'll Learn
Create a basic NestJS application instance
Add a configuration variable for allowed origin
Enable CORS with the allowed origin and credentials option
Complete the main bootstrap function with the CORS setup
💡 Why This Matters
🌍 Real World
Many web applications have frontend and backend on different domains. Configuring CORS correctly allows secure communication between them.
💼 Career
Backend developers often need to configure CORS in NestJS or other frameworks to enable frontend-backend integration without security issues.
Progress0 / 4 steps
1
Create a NestJS application instance
Write code to create a NestJS application instance called app using NestFactory.create(AppModule).
NestJS
Hint
Use const app = await NestFactory.create(AppModule); inside an async bootstrap function.
2
Add a configuration variable for allowed origin
Add a constant variable called allowedOrigin and set it to the string 'http://example.com'.
NestJS
Hint
Define const allowedOrigin = 'http://example.com'; inside the bootstrap function.
3
Enable CORS with allowed origin and credentials
Use app.enableCors() with an options object that sets origin to allowedOrigin and credentials to true.
NestJS
Hint
Call app.enableCors() with an object: { origin: allowedOrigin, credentials: true }.
4
Complete the bootstrap function by starting the server
Add await app.listen(3000); inside the bootstrap function to start the server on port 3000.
NestJS
Hint
Use await app.listen(3000); to start the server.
Practice
(1/5)
1. What is the main purpose of enabling CORS in a NestJS application?
easy
A. To encrypt data between client and server
B. To speed up the server response time
C. To manage database connections
D. To allow web browsers from specific origins to access the backend resources
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 D
Quick Check:
CORS controls access by origin = C [OK]
Hint: CORS controls which websites can access your backend [OK]
Common Mistakes:
Confusing CORS with encryption
Thinking CORS speeds up server
Mixing CORS with database management
2. Which of the following is the correct way to enable CORS with default settings in a NestJS app?
easy
A. app.setCors(true)
B. app.enableCors({ origin: '*' })
C. app.enableCors()
D. app.useCors()
Solution
Step 1: Recall NestJS CORS enabling method
The official method to enable CORS is app.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 C
Quick Check:
Default CORS enable = app.enableCors() [OK]
Hint: Use app.enableCors() to enable CORS simply [OK]
Common Mistakes:
Using incorrect method names like setCors or useCors
Passing invalid parameters to enableCors
Confusing CORS enabling with middleware usage
3. Given this NestJS code snippet, what will be the allowed origin for CORS?
B. The property 'origins' is incorrect; it should be 'origin'
C. enableCors does not accept any options
D. The URLs must be strings without 'https://'
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 B
Quick Check:
Correct property name is origin = A [OK]
Hint: Use 'origin' not 'origins' for CORS config [OK]
Common Mistakes:
Using 'origins' instead of 'origin'
Thinking enableCors rejects options
Removing protocol from URLs
5. You want to allow CORS only for GET and POST methods from two specific domains: https://app1.com and https://app2.com. Which is the correct NestJS CORS configuration?
hard
A. app.enableCors({ origin: ['https://app1.com', 'https://app2.com'], methods: ['GET', 'POST'] });
B. app.enableCors({ origins: ['https://app1.com', 'https://app2.com'], methods: 'GET,POST' });
C. app.enableCors({ origin: '*', methods: ['GET', 'POST'] });
D. app.enableCors({ origin: ['https://app1.com', 'https://app2.com'], method: ['GET', 'POST'] });
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.