Fix Express CORS Error — No 'Access-Control-Allow-Origin' Header
Express CORS errors are blocked by the browser, not caused by a server crash. Here's why they happen, how to configure the cors package correctly for production, handle preflight requests, send credentials, and debug CORS in 60 seconds.
Understanding What CORS Actually Is
CORS (Cross-Origin Resource Sharing) is enforced by the browser, not the server. Your API received the request fine. The browser saw the response didn't include Access-Control-Allow-Origin and blocked your JavaScript from reading it.
This means:
curland Postman work fine — they don't enforce CORS- The error is not a server crash — it's a browser security policy
- The fix is adding response headers to your Express server
The Fix: Use the cors Package
cors() with no arguments adds Access-Control-Allow-Origin: * to every response. Works in development. Too permissive for production.
Production Configuration
Warning: Never use
origin: '*'withcredentials: true. Browsers reject this combination. If you're sending cookies or auth headers, specify the exact origin.
Multiple Allowed Origins
Preflight Requests (OPTIONS)
Browsers send a preflight OPTIONS request before POST, PUT, DELETE, or requests with custom headers. Your server must respond to OPTIONS correctly or the actual request never fires.
The cors package handles this automatically if applied before your routes. If you're seeing the error only on non-GET requests, check middleware order:
Or handle preflight explicitly:
Sending Credentials (Cookies + Auth Headers)
If your frontend sends credentials: 'include' or an Authorization header:
Express backend must:
Note: If you see
The value of the 'Access-Control-Allow-Origin' header must not be the wildcard '*' when the request's credentials mode is 'include'— this is why. Switch from'*'to an exact origin.
Manual Header Approach (Without cors Package)
Diagnosing CORS Issues
CORS Error Checklist
| Symptom | Check |
|---|---|
| Works in Postman/curl, fails in browser | Classic CORS — add headers |
| Works for GET, fails for POST | Preflight OPTIONS not handled |
credentials: true not working | Using origin: '*' — must use exact origin |
Error on Authorization header | Add Authorization to allowedHeaders |
| Works locally, fails in production | ALLOWED_ORIGIN env var not set for prod domain |
For CORS errors in complex setups — nginx proxy in front of Express, multiple API subdomains, custom auth middleware that runs before CORS — paste your Express config and the full error into DebugAI. It identifies where in the middleware chain the headers are getting dropped.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.