Performance & Security

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.com

CORS Headers Reference

HeaderExample ValueNotes
Access-Control-Allow-Originhttps://myapp.com or *Exact origin required when credentials: true
Access-Control-Allow-MethodsGET, POST, PUT, DELETE, OPTIONSInclude OPTIONS for preflight
Access-Control-Allow-HeadersContent-Type, AuthorizationList every custom header client sends
Access-Control-Expose-HeadersX-RateLimit-RemainingResponse headers JS can read; default: none
Access-Control-Allow-CredentialstrueRequired when sending cookies; forbids * origin
Access-Control-Max-Age86400Seconds to cache preflight (avoid repeat OPTIONS)
VaryOriginTell 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

Using Access-Control-Allow-Origin: * with credentials: include — CORS error always
When sending credentials, you must set an exact origin. Wildcards are forbidden with credentials.
Trying to fix CORS on the client (browser extension, wrong fetch config) — doesn't work
CORS is controlled by the server. Fix it server-side: add the right headers in your backend middleware.
Forgetting the Vary: Origin header — CDN caches one CORS response and serves it to wrong origins
Add Vary: Origin when CORS origin is dynamic. This ensures CDNs store separate cached responses per origin.
Not including OPTIONS in Allow-Methods — preflight fails silently
Always include OPTIONS in Access-Control-Allow-Methods. Preflights use OPTIONS and must succeed first.

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

1 / 4
1

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.

1 of 4
⚠️CORS Is Enforced by the Browser, Not the Server
The same-origin check and the preflight handshake happen entirely in the browser. A server that omits CORS headers is still accessible from curl, Postman, or a server-to-server call — only browser-based fetch and XHR are restricted. Never rely on CORS alone as a security boundary.

🎯 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 OPTIONS request 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.