Express API CORS
Introduction
When developing modern web applications, you'll often have a frontend application on one domain making requests to your Express REST API on another domain. For example, your React application might be running on localhost:3000
, while your Express API runs on localhost:5000
. In production, your frontend might be hosted at example.com
while your API is at api.example.com
.
By default, browsers block these cross-origin HTTP requests due to the Same-Origin Policy, a critical security mechanism built into web browsers. This is where CORS (Cross-Origin Resource Sharing) comes in.
CORS is a mechanism that uses HTTP headers to tell browsers whether a specific web application can share resources with another web application from a different origin (domain, protocol, or port).