Quickly fix CORS errors in your API by enabling cross-origin requests with a simple middleware.
Problem:
You are getting this error in frontend:
"Access to fetch at 'API_URL' from origin 'http://localhost:3000' has been blocked by CORS policy"
Fix (1 line):
Install CORS:
npm install cors
Enable it in your Express app:
const cors = require('cors');
app.use(cors());That's it — your API will now accept requests from any origin.
---
More Secure Option (Recommended for production):
app.use(cors({
origin: 'http://localhost:3000'
}));You can also allow multiple domains:
app.use(cors({
origin: ['http://localhost:3000', 'https://yourdomain.com']
}));---
Bonus Tip:
If you're using credentials (cookies/auth), enable:
app.use(cors({
origin: 'http://localhost:3000',
credentials: true
}));More 1-Min Fixs
Fix “Too Many Re-renders” Error in React
This error happens when your component keeps updating state inside render, causing an infinite loop.
⚡Fix API Calls Running Twice in React Strict Mode
React Strict Mode can call effects twice in development. Add a guard or make your API action idempotent.
⚡Fix “map is not a function” Quickly
This usually happens when you think a value is an array, but it is actually undefined, null, or an object.
Get a new 1-Min Fix in your inbox
Subscribe to Stack Dev Life — free, no spam, unsubscribe anytime.
Subscribe free →