{"success":true,"name":"configure-cors-on-purpose-name-your-frontend-origins-instead","description":"Frontend calls to your API fail with CORS errors, or you are wiring the two together","category":"ai-brimind","path":"ai-brimind/configure-cors-on-purpose-name-your-frontend-origins-instead/SKILL.md","file":"SKILL.md","content":"---\nname: configure-cors-on-purpose-name-your-frontend-origins-instead\ndescription: \"Frontend calls to your API fail with CORS errors, or you are wiring the two together\"\n---\n\n# Configure CORS On Purpose: Name Your Frontend Origins Instead of Fighting or Wildcarding\n\n## When to use\nThe frontend and backend run on different origins (different ports like 3000→3005, or different domains), and you are either hitting 'blocked by CORS policy' errors, or about to 'fix' them with a copy-pasted wildcard allow-all.\n\n## Example request\nBrowser console shows 'blocked by CORS policy' → the request never had permission to be read cross-origin; this is the browser enforcing a rule, not a bug in your code — the fix belongs on the backend, deliberately. Panic-pasting origin '*' with credentials on → browsers refuse the combination, and even where wildcard works it means ANY website can call your API from its visitors' browsers; the allow-list of your real frontend origins is the actual fix. Adding CORS headers on the frontend requests → CORS is granted by the SERVER's response headers; no frontend header can grant it, which is why those StackOverflow snippets never work. Error appears only for POST with JSON but not GET → that is the preflight OPTIONS request being rejected; the backend must answer OPTIONS with the allowed methods and headers, and the middleware does this when configured properly.\n\n## Instructions\nAct as a developer who understands what CORS actually is before touching its config — it is the browser protecting USERS by refusing to let one website silently read responses from another site's API; it is not an obstacle, and 'making the error go away' with a wildcard is disabling a safety feature you will forget about. When wiring frontend to backend across origins, do this: (1) Name the exact origins that legitimately serve your frontend — e.g. http://localhost:3000 in dev, https://app.yourdomain.com in prod. Scheme, host, AND port each matter; localhost:3000 and [PRIVATE_HOST] are different origins, which is exactly why dev setups hit this. (2) Configure CORS on the backend with the standard middleware (the cors package in Express or your framework's equivalent) using an explicit allow-list of those origins — driven by environment config, so dev allows dev origins and prod allows prod ones, without code edits. Do not hand-write header lines when the middleware exists; the middleware also answers the preflight OPTIONS requests correctly, which hand-rolled code usually forgets. (3) If the frontend sends cookies or auth credentials cross-origin, set the credentials option on BOTH sides (backend credentials:true with a specific origin — wildcard is forbidden with credentials by the spec — and fetch/axios withCredentials on the frontend). Know that this pairing is the number one 'works in Postman, fails in browser' cause: Postman ignores CORS entirely, so the browser is the only honest tester here. (4) Read the actual browser console message before changing anything — it tells you WHICH part was refused (origin, method, header, credentials); fix that named thing instead of shotgunning config lines. (5) Wildcard '*' has one honest use: a truly public, credential-less API meant for any website. If that is not what you are building, it is the wrong answer. Only consider CORS done once you can say which origins are allowed and why each one, why the preflight succeeds, and why a random malicious website's JavaScript can NOT read your API from a visitor's browser — if your config cannot answer that last question, it is not configuration, it is surrender.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #209\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}