Full answer
An origin is the combination of scheme, host, and port. By default, a script from https://app.example may send requests to https://api.example, but the browser will not let it read the response. This same-origin policy protects users: otherwise any website could read your logged-in email or bank pages through your browser.
Cross-origin resource sharing (CORS) is a controlled exception. The server tells the browser which origins may read its responses by adding headers such as Access-Control-Allow-Origin: https://app.example. The browser compares this with the page's origin and either exposes the response or blocks it. Two facts clear up many misunderstandings. CORS is enforced by browsers only, so tools like curl and server code are not affected. And a blocked response does not mean the server ignored the request: a simple request, such as a form-style POST, may still have changed data on the server.
Simple requests (a few methods and headers, such as GET and a basic Content-Type) go straight out. Other requests, for example a PUT, or a POST with Content-Type: application/json, or a custom header, cause a preflight. The browser first sends an OPTIONS request with Origin, Access-Control-Request-Method, and Access-Control-Request-Headers. The server answers with Access-Control-Allow-Methods, Access-Control-Allow-Headers, and optionally Access-Control-Max-Age to cache the answer. Only then does the browser send the real request.
For cookies or HTTP authentication, the client sets credentials: 'include', and the server must reply with Access-Control-Allow-Credentials: true and a specific origin, because * is not allowed with credentials. When the allowed origin varies, add Vary: Origin so caches keep separate copies.
Common fixes are server-side headers or a same-origin proxy. Disabling CORS in the browser is not a fix for users.
const http = require('node:http');
const allowedOrigins = new Set(['https://app.example']);
const server = http.createServer((req, res) => {
const origin = req.headers.origin;
if (origin && allowedOrigins.has(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Vary', 'Origin');
}
if (req.method === 'OPTIONS') {
if (origin && allowedOrigins.has(origin)) {
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT');
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers'] || '');
res.setHeader('Access-Control-Max-Age', '600');
}
res.statusCode = 204;
return res.end();
}
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ secret: 'data' }));
});
function send(port, method, headers) {
return new Promise((resolve) => {
http.request({ host: '127.0.0.1', port, method, path: '/', headers }, (res) => {
res.resume();
res.on('end', () => {
const picked = Object.fromEntries(
Object.entries(res.headers).filter(([name]) => name.startsWith('access-control') || name === 'vary')
);
resolve({ status: res.statusCode, ...picked });
});
}).end();
});
}
server.listen(0, '127.0.0.1', async () => {
const port = server.address().port;
console.log('allowed origin GET:', await send(port, 'GET', { Origin: 'https://app.example' }));
console.log('other origin GET: ', await send(port, 'GET', { Origin: 'https://evil.example' }));
console.log('no origin GET: ', await send(port, 'GET', {}));
console.log('preflight: ', await send(port, 'OPTIONS', {
Origin: 'https://app.example',
'Access-Control-Request-Method': 'PUT',
'Access-Control-Request-Headers': 'content-type',
}));
server.close();
});Run with Node.js v24.22.1 as js-78.js; two runs gave identical output. Output: allowed origin GET: { status: 200, 'access-control-allow-origin': 'https://app.example', 'access-control-allow-credentials': 'true', vary: 'Origin' } other origin GET: { status: 200 } no origin GET: { status: 200 } preflight: { status: 204, 'access-control-allow-origin': 'https://app.example', 'access-control-allow-credentials': 'true', vary: 'Origin', 'access-control-allow-methods': 'GET, PUT', 'access-control-allow-headers': 'content-type', 'access-control-max-age': '600' }
Key points
- Origin is scheme, host, and port; same-origin policy blocks reading cross-origin responses.
- CORS is the server's opt-in via Access-Control headers; only browsers enforce it.
- Non-simple requests trigger an OPTIONS preflight before the real request.
- Credentials require a specific origin and Allow-Credentials: true; not the wildcard.
- Add Vary: Origin when the allowed origin changes per request.
Common mistakes
- Believing CORS protects the server. It protects users by limiting which pages can read responses.
- Using Access-Control-Allow-Origin: * together with credentials.
- Reflecting any Origin header back without checking an allow list.
- Fixing a CORS error by changing frontend code when the server headers are missing.
Follow-ups
- Which requests count as simple, and which trigger a preflight?
- Why does CORS not protect against CSRF?
- What does Access-Control-Expose-Headers do?
Tips
- Start from the same-origin policy and explain CORS as an opt-in; the story is then easy to follow.
- Say that the server still receives the request; many candidates miss it.
- Describe the preflight in two sentences with the three request headers.