Difficulty: Beginner
What is CORS, why does the browser enforce it, and how does a preflight request work?
CORS (Cross-Origin Resource Sharing) is a browser mechanism that lets a server explicitly allow web pages from other origins to read its responses. By default the Same-Origin Policy blocks JavaScript on https://app.example.in from reading a response from https://api.other.com; CORS response headers like Access-Control-Allow-Origin relax that rule for origins the server trusts. CORS is enforced by the browser, not the server, so it protects users, not your API.
An origin is the triple scheme + host + port. These are all different origins:
https://swiggy.in vs http://swiggy.in (scheme)https://swiggy.in vs https://api.swiggy.in (host)http://localhost:3000 vs http://localhost:5173 (port)That last one is why a React app on Vite's port 5173 calling an Express API on 3000 hits CORS errors in development.
A simple request (GET, HEAD or POST with only safe headers and a Content-Type of text/plain, application/x-www-form-urlencoded or multipart/form-data) is sent directly. The browser then checks the response's Access-Control-Allow-Origin and hides the response from JS if it does not match.
Anything else triggers a preflight: an OPTIONS request sent first, asking permission.
Origin: https://app.example.inAccess-Control-Request-Method: PUTAccess-Control-Request-Headers: content-type, authorizationThe server must reply with matching Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers. Access-Control-Max-Age lets the browser cache that answer. Sending Content-Type: application/json or an Authorization header always causes a preflight.
To send cookies cross-origin, the client sets credentials: 'include' and the server must return Access-Control-Allow-Credentials: true and a specific origin. The browser rejects Access-Control-Allow-Origin: * together with credentials.
The classic misconfiguration is reflecting whatever Origin arrives back into Access-Control-Allow-Origin while also allowing credentials. That lets any website read authenticated responses from your API. Always compare against an allowlist, and add Vary: Origin so caches and CDNs do not serve one origin's headers to another.
curl and Postman ignore it completely.server.proxy), never a client-side flag.import cors from 'cors';
const allowlist = new Set([
'https://app.example.in',
'http://localhost:5173', // dev only
]);
app.use(cors({
origin(origin, cb) {
// Allow same-origin / server-to-server calls with no Origin header
if (!origin || allowlist.has(origin)) return cb(null, true);
cb(null, false); // no CORS headers, browser blocks the read
},
credentials: true, // Access-Control-Allow-Credentials: true
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
maxAge: 600, // cache preflight for 10 minutes
}));
The `cors` package echoes the specific allowed origin and sets `Vary: Origin`, which is required when credentials are allowed.
const res = await fetch('https://api.example.in/profile', {
method: 'PUT',
credentials: 'include', // send cookies cross-origin
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Asha' }),
});
The JSON content type plus a PUT method makes the browser send a preflight first.
Same-Origin Policy, CORS, Preflight Request, Access-Control-Allow-Origin, Credentials