What does CORS stand for and why is it important in web development?
CORS stands for Cross-Origin Resource Sharing. It is important because it controls how resources on a web server can be requested from another domain, helping to keep web applications secure by preventing unauthorized access.
Click to reveal answer
beginner
How do you enable CORS in a NestJS application?
You enable CORS in NestJS by calling the enableCors() method on the app instance, usually in the main.ts file, like app.enableCors(). You can also pass options to customize the behavior.
Click to reveal answer
intermediate
What is the purpose of the origin option in NestJS CORS configuration?
The origin option specifies which domains are allowed to access the server resources. It can be a string, an array of strings, or a function to dynamically allow or deny requests from certain origins.
Click to reveal answer
intermediate
How can you restrict CORS to allow only specific HTTP methods in NestJS?
You can restrict allowed HTTP methods by setting the methods option in the enableCors() configuration, for example: app.enableCors({ methods: ['GET', 'POST'] }).
Click to reveal answer
beginner
What happens if you do not configure CORS in a NestJS API that is accessed from a different domain?
If CORS is not configured, browsers will block requests made from different domains for security reasons. This means your frontend app on another domain won't be able to access your NestJS API.
Click to reveal answer
Which NestJS method is used to enable CORS?
Aapp.enableCors()
Bapp.useCors()
Capp.setCors()
Dapp.configureCors()
✗ Incorrect
The correct method to enable CORS in NestJS is app.enableCors().
What does the 'origin' option in CORS configuration control?
AThe server port number
BWhich HTTP methods are allowed
CWhich domains can access the server
DThe response format
✗ Incorrect
The 'origin' option controls which domains are allowed to access the server resources.
If you want to allow only GET and POST requests from other domains, which option should you set?
AallowedMethods: ['GET', 'POST']
BcorsMethods: ['GET', 'POST']
ChttpMethods: ['GET', 'POST']
Dmethods: ['GET', 'POST']
✗ Incorrect
The correct option is 'methods' to specify allowed HTTP methods in CORS.
What will happen if CORS is not enabled on a NestJS API accessed from a different domain?
AThe API will work normally
BThe browser will block the request
CThe server will crash
DThe request will be redirected
✗ Incorrect
Browsers block cross-origin requests if CORS is not enabled for security.
Can the 'origin' option in NestJS CORS configuration accept a function?
AYes, to dynamically allow or deny origins
BNo, only strings are allowed
CNo, only arrays are allowed
DYes, but only for logging purposes
✗ Incorrect
The 'origin' option can be a function to dynamically control allowed origins.
Explain how to enable and customize CORS in a NestJS application.
Think about the main.ts file and the enableCors method.
You got /4 concepts.
Describe what problems CORS solves and what happens if it is not configured properly in NestJS.
Consider browser security and cross-origin requests.
You got /4 concepts.
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.