CORS Explained: Why Your API Works in Postman but Fails in the Browser
In this article
What is CORS? The same-origin rule What is a preflight request? Fix CORS in Express.js Cookies and credentials Common mistakes Debugging checklistYou build an API. You test it in Postman. The response is beautiful JSON. Status code: 200 OK. You feel unstoppable.
Then your frontend makes the same request:
Access to fetch at 'https://api.example.com/users'
from origin 'http://localhost:3000' has been blocked by CORS policy.
The API is online. The URL is correct. Postman works. So what is broken?
What is CORS?
CORS stands for Cross-Origin Resource Sharing. It is a browser mechanism that lets a server say which websites are allowed to read its responses.
Think of an API as an office building. The browser checks whether the website making the request is on the visitor list. The server provides that list using HTTP response headers.
For example, an API may return:
Access-Control-Allow-Origin: https://my-frontend.com
This tells the browser that JavaScript running on https://my-frontend.com may read the response. It does not mean that the API is private or that other clients cannot send requests.
The same-origin rule
Two URLs have the same origin only when their scheme, host, and port all match.
http://localhost:3000
http://localhost:4000
These are different origins because the ports differ. This is also true for:
httpversushttpsapp.example.comversusapi.example.comlocalhost:3000versuslocalhost:5173
When browser JavaScript requests a resource from another origin, the browser applies CORS rules to decide whether the calling page can access the response.
What is a preflight request?
Some cross-origin requests can be sent directly. Others make the browser ask permission first with an automatic OPTIONS request. This is called a preflight.
A preflight is commonly triggered by methods such as PUT or DELETE, custom request headers, or a content type such as application/json.
The browser may send something like this before the real request:
OPTIONS /users HTTP/1.1
Origin: http://localhost:3000
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, authorization
The server needs to answer with permission, for example:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
OPTIONS request in the browser Network panel, not only the request you expected to send.Fix CORS in Express.js
For an Express API, the cors middleware is a straightforward way to add the required response headers and handle preflight requests.
Install it:
npm install cors
Then allow the exact frontend origin:
import express from 'express';
import cors from 'cors';
const app = express();
app.use(cors({
origin: 'http://localhost:3000',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use(express.json());
app.get('/api/users', (req, res) => {
res.json([{ id: 1, name: 'Ada' }]);
});
app.listen(4000);
For production, replace the local development origin with your real frontend origin. If you support more than one trusted frontend, validate against an explicit allowlist rather than reflecting every origin.
Cookies and credentials
If your frontend uses cookies or other browser credentials, both sides must opt in.
On the frontend:
fetch('https://api.example.com/profile', {
credentials: 'include'
});
On the Express server:
app.use(cors({
origin: 'https://my-frontend.com',
credentials: true
}));
* as the allowed origin. Use the exact trusted origin. CORS is not authentication — your API must still verify the user and authorize every protected action.Common CORS mistakes
1. Using a wildcard with credentials
Access-Control-Allow-Origin: * cannot be combined with credentialed browser requests. Return the specific trusted origin instead.
2. Allowing the origin but not the headers
If your frontend sends Authorization or Content-Type, make sure the preflight response allows those headers.
3. Configuring CORS after your routes
Register the CORS middleware before routes that need it, so the relevant responses include the CORS headers.
4. Trying to fix it in frontend JavaScript
The server must send the appropriate headers. Adding mode: 'no-cors' is not a general fix: it produces an opaque response that your JavaScript cannot read.
CORS debugging checklist
✓ Is the exact frontend origin allowed (including scheme and port)?
✓ Does the preflight
OPTIONS request succeed?✓ Are the requested method and headers allowed?
✓ If using cookies, is credentials support enabled on both sides?
✓ Are CORS headers present on error responses too?
✓ Is the API authenticating requests independently of CORS?
Use the browser DevTools Network panel to inspect the request and response headers. The console message tells you that the browser blocked access; the Network panel helps you find which permission is missing.