Express.js / Editorial solution guide

Browser cannot read an Express API response

Diagnose CORS middleware placement, preflight and proxy responses while preserving server-side access controls.

About 3 min readContent reviewed

Read-only knowledge base ยท No public comments

Recognize the symptoms

Browser JavaScript reports a cross-origin error although a direct HTTP client receives a response. The request may stop at OPTIONS or the actual response may lack the expected headers. Record the exact frontend origin, requested method and safe header names, but do not copy cookies or authorization values into the report.

Understand the likely causes

  • The CORS middleware may be mounted after the route or omitted for a failure path, the origin or requested headers may not match policy, or a proxy may generate the response before Express. Credentialed browser requests also depend on an explicit compatible origin and cookie policy. A console message alone cannot identify the rejecting layer.

1. Inspect preflight and actual response

Use the browser network panel in a controlled environment to identify an OPTIONS preflight, its status and response headers, and whether the actual request followed. Compare the Origin, Access-Control-Request-Method and requested header names with the intended policy. A command-line client does not enforce browser CORS and cannot by itself prove that frontend JavaScript can read the response.

2. Trace middleware and proxy

Inspect where the Express CORS middleware is mounted relative to routers and error handling. The documented application-level cors middleware handles preflight for routes it covers; route-specific use requires appropriate preflight handling. Check whether the proxy returned the failure before the request reached Express. Distinguish a missing header on an application error from an incorrect origin policy rather than adding a blanket header at an unrelated layer.

3. Apply the intended policy

Configure the approved origins, methods, headers and credential behavior required by the application, and ensure the relevant response paths receive the policy. Match scheme, hostname and port for each origin. Do not reflect arbitrary origins or broaden access merely to silence the browser. CORS controls browser access to responses; authentication, object authorization and CSRF protection still require independent server-side decisions.

4. Verify allowed and denied cases

Repeat the preflight and actual request through the intended proxy using an approved origin. Check a disallowed origin in a browser and separately test that an unauthenticated or unauthorized direct client cannot access protected data. Inspect successful and error responses, and verify cookie behavior when credentials are required. Keep a safe origin-policy matrix and regression tests for domain, port and middleware-order changes.

Verify the fix

Test allowed and disallowed origins in a browser and verify the preflight and actual response headers through the proxy. Independently test server-side authentication and authorization.

Keep it from recurring

  • Document exact allowed origins and middleware placement, and retain browser and direct-client regression cases. Avoid logging credentials or treating CORS as an access-control substitute.

Official references