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
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?