StackDevLife
๐Ÿ›Bug of the DayBeginnerNode.js / Express

API Working in Postman but Failing in Browser

~1 min read
๐Ÿ›TL;DR

API works in Postman but fails in browser due to missing CORS configuration.

The Bug

API works in Postman but fails in browser with CORS error

Root Cause

Browser blocks request because server does not allow cross-origin access

The Fix

You test your API in Postman โ€” everything works fine.
Then you call it from your frontend โ€” suddenly it fails.

Error:
Access to fetch at 'API_URL' from origin 'localhost' has been blocked by CORS policy

Why this happens:
Postman does not enforce browser security rules, but browsers do.

Root cause:
Your backend is not allowing cross-origin requests.

Fix in Node.js (Express):

JavaScript
const cors = require("cors");
app.use(cors());

Or configure specific origin:

JavaScript
app.use(cors({
origin: "http://localhost:3000"
}));

After adding this, your API will work correctly in the browser.

corsapibackenddebugging
๐Ÿ›

Get a new Bug of the Day in your inbox

Subscribe to Stack Dev Life โ€” free, no spam, unsubscribe anytime.

Subscribe free โ†’