API Fundamentals Course
API Fundamentals
/
Intermediate

CORS

Definition

Cross-Origin Resource Sharing. A browser security mechanism that restricts web pages from making HTTP requests to a different domain than the one that served the web page, unless the server explicitly allows it.

Explain Like I'm New

Imagine you live at `localhost:3000`. You try to borrow sugar from your neighbor at `api.com`. The browser acts as a security guard and stops you, saying 'You don't live there.' To get the sugar, the neighbor (`api.com`) must put a sign on their door (a Header) that explicitly says 'I allow `localhost:3000` to borrow sugar'.

Real World Example

You build a React app on Vercel and a Node API on Heroku. When React tries to fetch the API, the browser blocks it and throws a massive red CORS error. You must configure the Node API to send the `Access-Control-Allow-Origin` header.

Common Use Cases

  • Frontend-to-Backend communication
  • Third-party APIs

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Does a CORS error mean the backend server crashed?

intermediate

  • If you bypass the browser and make a request using Postman or cURL, will you get a CORS error?

Flash Cards

Question

Did server crash?

Click to reveal answer
Answer

No. A CORS error is purely a client-side security mechanism enforced by the web browser. The server actually received the request perfectly fine, but the browser refused to let your JavaScript read the response.

Question

Postman CORS?

Click to reveal answer
Answer

No! Postman is not a web browser. It does not enforce CORS policies. CORS only exists to protect users running untrusted JavaScript inside a web browser environment.