Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does CORS stand for and what is its main purpose?
CORS stands for Cross-Origin Resource Sharing. Its main purpose is to allow or block web pages from making requests to a different domain than the one that served the web page, protecting users from malicious sites.
Click to reveal answer
beginner
Why do browsers enforce CORS policies on API requests?
Browsers enforce CORS to prevent malicious websites from reading sensitive data from another site without permission. It acts like a security guard checking if cross-site requests are allowed.
Click to reveal answer
intermediate
How does an Express API enable CORS to allow requests from other domains?
In Express, you can enable CORS by using the 'cors' middleware. This middleware adds the right headers to responses so browsers know which domains are allowed to access the API.
Click to reveal answer
beginner
What happens if an API does not handle CORS properly?
If an API does not handle CORS, browsers will block requests from other domains, causing errors in web apps trying to use that API from a different site.
Click to reveal answer
beginner
Give a simple example of enabling CORS in an Express app.
You can enable CORS by installing the 'cors' package and adding it as middleware: <br><code>const cors = require('cors');<br>app.use(cors());</code><br>This allows all domains to access the API.
Click to reveal answer
What does CORS protect users from?
ABrowser crashes
BSlow internet connections
CMalicious websites accessing data from other domains
DIncorrect API responses
✗ Incorrect
CORS protects users by blocking unauthorized cross-domain requests that could steal data.
Which HTTP header is commonly used to control CORS permissions?
AUser-Agent
BContent-Type
CAuthorization
DAccess-Control-Allow-Origin
✗ Incorrect
The 'Access-Control-Allow-Origin' header tells the browser which domains can access the resource.
In Express, which package helps to easily enable CORS?
Aexpress-cors
Bcors
Cbody-parser
Dhelmet
✗ Incorrect
The 'cors' package is the standard middleware to enable CORS in Express apps.
What will happen if a browser blocks a cross-origin request due to CORS?
AThe request fails and the web app cannot access the response
BThe request succeeds but with a warning
CThe browser crashes
DThe server ignores the request
✗ Incorrect
Browsers block the request and prevent the web app from reading the response.
Which of these is NOT a reason to configure CORS on an API?
AImprove API response speed
BPrevent unauthorized cross-site requests
CAllow trusted websites to access the API
DControl which domains can use the API
✗ Incorrect
CORS controls access permissions, it does not affect response speed.
Explain in simple terms why CORS is important for APIs and how it protects users.
Think about how websites can try to get data from other sites without permission.
You got /4 concepts.
Describe how you would enable CORS in an Express API and why you might want to do that.
Consider what happens when a web app on one domain tries to call your API on another domain.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason CORS is important for APIs in Express?
easy
A. It encrypts the data sent by the API.
B. It speeds up the API response time.
C. It automatically logs all API requests.
D. It controls which websites can access your API to protect it.
Solution
Step 1: Understand CORS purpose
CORS stands for Cross-Origin Resource Sharing and it controls which websites can call your API.
Step 2: Identify protection role
By controlling access, CORS protects your API from unwanted or malicious websites.
Final Answer:
It controls which websites can access your API to protect it. -> Option D
Quick Check:
CORS controls access = D [OK]
Hint: Remember: CORS controls access, not speed or encryption [OK]
Common Mistakes:
Thinking CORS speeds up API
Confusing CORS with logging
Believing CORS encrypts data
2. Which Express middleware is commonly used to enable CORS?
easy
A. cors
B. express-session
C. body-parser
D. morgan
Solution
Step 1: Recall Express middleware for CORS
The npm package named 'cors' is the standard middleware to enable CORS in Express.