0
0
Expressframework~10 mins

Why CORS matters for APIs in Express - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why CORS matters for APIs
Browser sends API request
Server receives request
Server checks Origin header
Origin allowed?
NoReject request with CORS error
Server sends response with CORS headers
Browser accepts response if CORS headers valid
API data accessible in browser
The browser sends a request with an origin. The server checks if the origin is allowed. If yes, it sends back CORS headers allowing access. Otherwise, the browser blocks the response.
Execution Sample
Express
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', 'https://example.com');
  next();
});
This code adds a header to allow requests only from https://example.com.
Execution Table
StepActionRequest OriginServer CORS CheckResponse Header SentBrowser Behavior
1Browser sends API requesthttps://example.comCheck if origin allowedAccess-Control-Allow-Origin: https://example.comBrowser accepts response
2Browser sends API requesthttps://malicious.comCheck if origin allowedNo CORS header or different originBrowser blocks response (CORS error)
3Browser sends API requestnull (local file)Check if origin allowedDepends on server configBrowser blocks or accepts based on header
4Request without Origin header (e.g. curl)No OriginNo CORS check neededNo CORS header neededResponse received normally
5End of flow---Browser enforces CORS policy based on headers
💡 Browser blocks API response if CORS headers do not allow the request origin.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Request Originundefinedhttps://example.comhttps://malicious.comnullNo Originvaries per request
Server CORS Headernone'https://example.com'none or differentdependsnonevaries per request
Browser Accepts Responsefalsetruefalsedependstruedepends on CORS headers
Key Moments - 3 Insights
Why does the browser block the response from https://malicious.com?
Because the server did not send an Access-Control-Allow-Origin header matching https://malicious.com, so the browser enforces CORS and blocks access (see execution_table step 2).
Why do requests without an Origin header (like curl) not get blocked?
CORS is a browser security feature. Requests without Origin header are usually from non-browser clients and are not blocked (see execution_table step 4).
What happens if the server allows all origins with '*'?
The server sends Access-Control-Allow-Origin: * header, so browsers accept responses from any origin, allowing cross-origin access.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the browser behavior when the request origin is https://example.com?
ABrowser ignores the response
BBrowser blocks the response due to CORS error
CBrowser accepts the response
DBrowser retries the request
💡 Hint
Check execution_table row 1 under 'Browser Behavior'
At which step does the browser block the response due to missing or wrong CORS headers?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table row 2 for browser blocking
If the server sets Access-Control-Allow-Origin to '*', how would the browser behave for https://malicious.com origin?
ABrowser blocks the response
BBrowser accepts the response
CBrowser sends a new request
DBrowser ignores the origin
💡 Hint
Recall that '*' allows all origins, so browser accepts (see key_moments answer 3)
Concept Snapshot
Why CORS matters for APIs:
- Browsers enforce CORS to protect users from unauthorized cross-origin requests.
- Server must send Access-Control-Allow-Origin header matching the request origin.
- Without correct CORS headers, browser blocks API responses.
- Non-browser clients (curl) are not affected by CORS.
- Setting '*' allows any origin to access the API.
Full Transcript
When a browser sends a request to an API, it includes the origin of the page making the request. The server checks this origin and decides if it is allowed to access the API. If allowed, the server sends back a special header called Access-Control-Allow-Origin with the allowed origin. The browser then lets the page access the API response. If the origin is not allowed or the header is missing, the browser blocks the response to protect the user. This is called CORS, or Cross-Origin Resource Sharing. It is important because it stops malicious websites from reading data from APIs they shouldn't access. Non-browser tools like curl do not have this restriction. Developers must configure their API servers to send the right CORS headers to allow trusted websites to use their APIs safely.