0
0
Node.jsframework~5 mins

CORS configuration in Node.js - 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 Node.js Express app using the 'cors' package?
You install the 'cors' package and then use it as middleware in your Express app like this: <br><code>const cors = require('cors');<br>app.use(cors());</code><br>This allows all cross-origin requests by default.
Click to reveal answer
intermediate
What is the purpose of setting specific origins in CORS configuration?
Setting specific origins restricts which domains can access your server resources. This improves security by only allowing trusted websites to make requests.
Click to reveal answer
intermediate
Explain the difference between simple requests and preflight requests in CORS.
Simple requests are basic HTTP requests that browsers allow without extra checks. Preflight requests are OPTIONS requests sent by the browser to check if the actual request is safe to send, used for methods like PUT or DELETE or when custom headers are involved.
Click to reveal answer
intermediate
How can you configure CORS in Express to allow only GET and POST methods from 'https://example.com'?
You can configure CORS like this:<br>app.use(cors({<br> origin: 'https://example.com',<br> methods: ['GET', 'POST']<br>}));<br>This restricts access to only GET and POST requests from that origin.
Click to reveal answer
What does the 'Access-Control-Allow-Origin' header do?
ASpecifies which origins can access the resource
BBlocks all cross-origin requests
CEncrypts data sent between client and server
DSpecifies the HTTP methods allowed
Which HTTP method is used for CORS preflight requests?
AOPTIONS
BPOST
CGET
DPUT
In Express, which package is commonly used to handle CORS?
Aexpress-cors
Bcors-handler
Ccross-origin
Dcors
What happens if you do not configure CORS on your server?
AAll cross-origin requests are allowed by default
BYour server sends an error response
CBrowsers block cross-origin requests by default
DYour server crashes
Which of these is NOT a valid CORS configuration option in the 'cors' package?
Amethods
Bencryption
Ccredentials
Dorigin
Describe how to set up CORS in a Node.js Express app to allow only requests from 'https://myapp.com' and only GET and POST methods.
Think about the options object passed to the cors middleware.
You got /4 concepts.
    Explain why browsers use preflight requests and how they relate to CORS configuration.
    Consider what happens before the main request is sent.
    You got /4 concepts.