Challenge - 5 Problems
CORS Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the effect of this CORS configuration in NestJS?
Consider this NestJS setup for CORS:
What will happen if a browser tries to send a PUT request from https://example.com?
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);
Attempts:
2 left
💡 Hint
Check which HTTP methods are allowed in the CORS config.
✗ Incorrect
Only GET and POST methods are allowed by CORS. PUT is not allowed, so the browser blocks the request due to CORS policy.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Check the type of the 'origin' option for multiple domains.
✗ Incorrect
The 'origin' option accepts an array of allowed origins. Option C correctly uses an array.
🔧 Debug
advanced2:00remaining
Why does this NestJS CORS config cause all requests to fail?
Given this code:
All requests from any origin fail with CORS error. Why?
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'));
}
}
});Attempts:
2 left
💡 Hint
Check how the callback signals denial in CORS origin function.
✗ Incorrect
To deny CORS, callback should be called with (null, false). Passing an Error causes failure.
❓ state_output
advanced2:00remaining
What is the value of the 'Access-Control-Allow-Credentials' header with this config?
In NestJS, you configure CORS as:
What will be the value of the 'Access-Control-Allow-Credentials' header in the response?
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
});Attempts:
2 left
💡 Hint
Check if the header is sent when credentials is false.
✗ Incorrect
When credentials is false, the header is not included in the response.
🧠 Conceptual
expert2:00remaining
Which statement about NestJS CORS configuration is TRUE?
Select the true statement about how NestJS handles CORS configuration:
Attempts:
2 left
💡 Hint
Think about dynamic origin checks and default behaviors.
✗ Incorrect
Using a function for 'origin' lets you check each request's origin dynamically and decide if allowed.