0
0
NestJSframework~5 mins

CORS configuration in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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()
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
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']
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
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
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.