CORS
Cross-Origin Resource Sharing is the browser mechanism that controls how JavaScript can make requests to a different domain. Understand the same-origin policy, simple vs preflighted requests, credentialed requests, and how to configure CORS correctly on both client and server.
What is CORS and how do I fix a CORS error?
CORS (Cross-Origin Resource Sharing) is a browser security rule: a page can only read responses from a different origin (domain, port or protocol) if that server says it's allowed, with headers like Access-Control-Allow-Origin. A CORS error is fixed on the server you're calling, not in your front-end code, or by routing the request through your own backend.
// Express server
import cors from "cors";
app.use(cors({ origin: "https://www.compileum.com", credentials: true }));
// Response header the browser needs to see
Access-Control-Allow-Origin: https://www.compileum.comCORS Headers Reference
| Header | Example Value | Notes |
|---|---|---|
| Access-Control-Allow-Origin | https://myapp.com or * | Exact origin required when credentials: true |
| Access-Control-Allow-Methods | GET, POST, PUT, DELETE, OPTIONS | Include OPTIONS for preflight |
| Access-Control-Allow-Headers | Content-Type, Authorization | List every custom header client sends |
| Access-Control-Expose-Headers | X-RateLimit-Remaining | Response headers JS can read; default: none |
| Access-Control-Allow-Credentials | true | Required when sending cookies; forbids * origin |
| Access-Control-Max-Age | 86400 | Seconds to cache preflight (avoid repeat OPTIONS) |
| Vary | Origin | Tell CDNs to cache different responses per Origin |
Request Types
Simple Request
GET, POST, HEAD with basic headers and form content types
Browser → Server (1 round trip)
- • GET /data
- • POST with text/plain or form body
- • HEAD /resource
Preflighted Request
PUT/PATCH/DELETE, JSON body, custom headers like Authorization
Browser OPTIONS → Server → Browser → Server (2 round trips)
- • POST with application/json
- • PUT or DELETE requests
- • Custom headers: Authorization, X-*
Credentialed Request
fetch(..., { credentials: 'include' }) — sends cookies
Requires Allow-Credentials: true + exact Allow-Origin
- • Session cookies cross-origin
- • Auth APIs with cookies
- • Subdomain sharing sessions
Common Pitfalls
Interactive Demo
Code Editor
<button id="ok">Fetch an API that allows CORS</button>
<button id="blocked">Fetch a site that doesn't</button>
<pre id="out"></pre>Preview
CORS — step by step
Same-Origin Policy
Browsers enforce that a page at one origin (scheme + host + port) cannot read responses from a different origin. This prevents malicious scripts from silently reading your bank page, but it also blocks legitimate cross-origin API calls.
🎯 Quick Check
When does the browser automatically send a preflight OPTIONS request?
CORS: summary
Key points
- Requests with custom headers or JSON bodies trigger a preflight
OPTIONSrequest first. Access-Control-Allow-Origin: *can't be combined with credentials (cookies).- CORS only applies in browsers; server-to-server requests aren't affected.
mode: "no-cors"doesn't fix anything; it just gives you an unreadable response.
Frequently asked questions
›Why does my request work in Postman but not in the browser?
Postman doesn't enforce CORS; browsers do. The server must send the right Access-Control-Allow-* headers for your origin.