โ What is CORS?
CORS stands for Cross-Origin Resource Sharing. It's a security mechanism implemented by browsers to restrict web pages from making requests to a different domain than the one that served the web page.
For example:
Frontend (React): http://localhost:3000
Backend (API): http://localhost:5000By default, a frontend served from port 3000 cannot make a request to port 5000 unless the backend explicitly allows it.
โ ๏ธ Why Does the CORS Error Occur?
When a frontend JavaScript application (like React) tries to fetch or POST data to a different domain or port, the browser checks if the backend server has allowed that origin.
If not, you'll see this error:
Access to fetch at 'http://localhost:5000/api/data' from origin 'http://localhost:3000' has been blocked by CORS policy...๐ฅ Common CORS Issues in React Projects
- CORS error while calling API on development
- Missing headers on backend (Access-Control-Allow-Origin)
- Preflight OPTIONS requests failing
- Authentication with cookies across domains
- Using third-party APIs with no CORS support
๐ Solution 1: Fixing CORS in Node.js/Express Backend
Install the cors middleware:
npm install corsIn your server.js or app.js:
const express = require('express');
const cors = require('cors');
const app = express();
// Allow specific origin
app.use(cors({
origin: 'http://localhost:3000',
credentials: true
}));
app.get('/api/data', (req, res) => {
res.json({ message: 'Hello from backend!' });
});
app.listen(5000, () => console.log('Server running on port 5000'));โ Handling Preflight Requests
Browsers send a preflight OPTIONS request when the request method is not GET/POST or uses custom headers.
Ensure Express handles it:
app.options('*', cors()); // Preflight๐ With Credentials (JWT/Auth)
If you're sending cookies (e.g., JWT token stored in cookies):
Frontend:
fetch('http://localhost:5000/api/protected', {
method: 'GET',
credentials: 'include'
});Backend:
app.use(cors({
origin: 'http://localhost:3000',
credentials: true
}));Also ensure you're using cookie-parser:
npm install cookie-parser๐งช Solution 2: Use Proxy in React (Development Only)
In package.json of React:
"proxy": "http://localhost:5000"Now React will proxy API calls, removing the need for CORS in dev.
// No need to specify full URL
fetch('/api/data');โ Pros: Easy in development โ Cons: Not usable in production
๐ง Solution 3: CORS Fix for 3rd-Party APIs (Proxy Server)
If you're calling an API without CORS support, use a proxy server like:
https://cors-anywhere.herokuapp.com/
fetch('https://cors-anywhere.herokuapp.com/https://example.com/api')Or use a self-hosted proxy:
npm install express-http-proxyโ ๏ธ Common Mistakes to Avoid
- Setting
*forAccess-Control-Allow-Originwhen using credentials (this will fail) - Forgetting to include
credentials: trueinfetch() - Not handling OPTIONS requests
- Misconfigured reverse proxies (Nginx/AWS API Gateway)
๐ง Bonus: CORS in Production
- Always whitelist specific domains (never use
*). - Use
helmetor similar packages for HTTP header protection. - Configure your reverse proxy (like Nginx) to handle CORS properly.
Example (Nginx):
location /api/ {
add_header 'Access-Control-Allow-Origin' 'https://yourdomain.com';
add_header 'Access-Control-Allow-Credentials' 'true';
}โจ Final Thoughts
CORS is a security feature, not a bug. Instead of disabling it recklessly, understand how to configure your backend correctly.
TL;DR:
- Use
corsmiddleware in Node.js - Use
proxyin React for local development - Be cautious with credentials and
Access-Control-Allow-Origin - Handle preflight OPTIONS properly