Bird
Raised Fist0
NestJSframework~20 mins

CORS configuration in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CORS Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the effect of this CORS configuration in NestJS?
Consider this NestJS setup for CORS:
const app = await NestFactory.create(AppModule);
app.enableCors({
  origin: 'https://example.com',
  methods: 'GET,POST',
  credentials: true
});
await app.listen(3000);

What will happen if a browser tries to send a PUT request from https://example.com?
NestJS
const app = await NestFactory.create(AppModule);
app.enableCors({
  origin: 'https://example.com',
  methods: 'GET,POST',
  credentials: true
});
await app.listen(3000);
AThe PUT request will be redirected to GET automatically.
BThe PUT request will succeed because origin matches.
CThe PUT request will succeed but credentials will be ignored.
DThe PUT request will be blocked by CORS policy.
Attempts:
2 left
💡 Hint
Check which HTTP methods are allowed in the CORS config.
📝 Syntax
intermediate
2:00remaining
Which option correctly enables CORS for multiple origins in NestJS?
You want to allow CORS requests from 'https://site1.com' and 'https://site2.com'. Which code snippet correctly configures this?
Aapp.enableCors({ origin: /https:\/\/site[12]\.com/ });
Bapp.enableCors({ origin: 'https://site1.com, https://site2.com' });
Capp.enableCors({ origin: ['https://site1.com', 'https://site2.com'] });
Dapp.enableCors({ origin: true });
Attempts:
2 left
💡 Hint
Check the type of the 'origin' option for multiple domains.
🔧 Debug
advanced
2:00remaining
Why does this NestJS CORS config cause all requests to fail?
Given this code:
app.enableCors({
  origin: (origin, callback) => {
    if (origin === 'https://allowed.com') {
      callback(null, true);
    } else {
      callback(new Error('Not allowed by CORS'));
    }
  }
});

All requests from any origin fail with CORS error. Why?
NestJS
app.enableCors({
  origin: (origin, callback) => {
    if (origin === 'https://allowed.com') {
      callback(null, true);
    } else {
      callback(new Error('Not allowed by CORS'));
    }
  }
});
AThe callback must be called with false to deny, not an Error.
BThe origin check is case-sensitive and fails for 'https://allowed.com'.
CThe origin function must return a boolean, not use callback.
DThe callback error should be null to deny access, not an Error object.
Attempts:
2 left
💡 Hint
Check how the callback signals denial in CORS origin function.
❓ state_output
advanced
2:00remaining
What is the value of the 'Access-Control-Allow-Credentials' header with this config?
In NestJS, you configure CORS as:
app.enableCors({
  origin: 'https://client.com',
  credentials: false
});

What will be the value of the 'Access-Control-Allow-Credentials' header in the response?
NestJS
app.enableCors({
  origin: 'https://client.com',
  credentials: false
});
AThe header will be 'Access-Control-Allow-Credentials: true'.
BThe header will be missing (not sent).
CThe header will be 'Access-Control-Allow-Credentials: false'.
DThe header will be 'Access-Control-Allow-Credentials: null'.
Attempts:
2 left
💡 Hint
Check if the header is sent when credentials is false.
🧠 Conceptual
expert
2:00remaining
Which statement about NestJS CORS configuration is TRUE?
Select the true statement about how NestJS handles CORS configuration:
AUsing a function for 'origin' allows dynamic origin validation per request.
BSetting 'origin' to true allows requests from any origin and sends credentials by default.
CThe 'methods' option defaults to all HTTP methods if omitted.
DSetting 'credentials' to true automatically sets 'Access-Control-Allow-Origin' to '*'.
Attempts:
2 left
💡 Hint
Think about dynamic origin checks and default behaviors.

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

  1. Step 1: Understand CORS purpose

    CORS (Cross-Origin Resource Sharing) controls which websites can access backend resources from a different origin.
  2. Step 2: Identify correct purpose in NestJS

    Enabling CORS in NestJS allows specific web origins to access the backend safely.
  3. Final Answer:

    To allow web browsers from specific origins to access the backend resources -> Option D
  4. 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

  1. Step 1: Recall NestJS CORS enabling method

    The official method to enable CORS is app.enableCors() without parameters for default settings.
  2. Step 2: Check options for correctness

    Options B, C, and D are not valid NestJS methods or have incorrect syntax.
  3. Final Answer:

    app.enableCors() -> Option C
  4. 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?
app.enableCors({ origin: 'https://example.com' });
medium
A. Only https://example.com can access the backend
B. All websites can access the backend
C. No websites can access the backend
D. Only localhost can access the backend

Solution

  1. Step 1: Analyze the origin option in enableCors

    The origin option is set to 'https://example.com', which restricts access to this exact origin.
  2. Step 2: Understand CORS behavior with origin setting

    Only requests coming from 'https://example.com' will be allowed by CORS policy.
  3. Final Answer:

    Only https://example.com can access the backend -> Option A
  4. Quick Check:

    origin restricts access = B [OK]
Hint: Origin option limits access to specified URL only [OK]
Common Mistakes:
  • Assuming origin '*' is default
  • Thinking multiple origins allowed without array
  • Confusing origin with methods or headers
4. Identify the error in this NestJS CORS configuration code:
app.enableCors({ origins: ['https://site1.com', 'https://site2.com'] });
medium
A. The array syntax is invalid for origins
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

  1. Step 1: Check property name for allowed origins

    The correct property name is 'origin' (singular), not 'origins'.
  2. Step 2: Confirm array usage for multiple origins

    Passing an array to 'origin' is valid to allow multiple origins.
  3. Final Answer:

    The property 'origins' is incorrect; it should be 'origin' -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    app.enableCors({ origin: ['https://app1.com', 'https://app2.com'], methods: ['GET', 'POST'] }); -> Option A
  4. Quick Check:

    Correct keys and array values = D [OK]
Hint: Use 'origin' and 'methods' with arrays for multiple values [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard