惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
The Cloudflare Blog
D
DataBreaches.Net
J
Java Code Geeks
G
Google Developers Blog
L
LangChain Blog
N
Netflix TechBlog - Medium
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
小众软件
小众软件
量子位
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
博客园_首页
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
腾讯CDC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
If Everything Is Running on Localhost, Why Do We Still Ge...
Khushi Patel · 2026-06-19 · via DEV Community

Khushi Patel

One question almost every frontend developer asks at some point:

"My frontend is running on localhost:3000 and my backend is running on localhost:5000. Both are on my own machine. Why am I still getting a CORS error?"

It feels strange because both applications are literally running on the same laptop.

The answer lies in how browsers define an Origin.


What Is an Origin?

An origin is made up of:

Protocol + Domain + Port

For example:

http://localhost:3000

and

http://localhost:5000

have:

  • Same protocol ✅
  • Same domain ✅
  • Different ports ❌

Because the ports are different, the browser treats them as two different origins.


What Happens During a Request?

Imagine your React app is running on:

http://localhost:3000

and it calls:

fetch("http://localhost:5000/users");

From the browser's perspective:

Origin A → localhost:3000
Origin B → localhost:5000

This is considered a cross-origin request.


Why Does the Browser Care?

Imagine if websites could freely call APIs from any origin.

A malicious website could:

  • Read your banking data
  • Access your private APIs
  • Perform actions on your behalf

To prevent this, browsers enforce the Same-Origin Policy.

By default:

Website A cannot access Website B's resources

unless Website B explicitly allows it.


How Does CORS Solve This?

CORS stands for:

Cross-Origin Resource Sharing

It is simply a mechanism that lets the server tell the browser:

"Yes, I trust requests coming from this origin."

Example response header:

Access-Control-Allow-Origin: http://localhost:3000

Now the browser allows the frontend to access the response.


Important Thing To Understand

The backend usually receives the request successfully.

The CORS error is often raised by the browser after receiving the response.

The flow looks like:

Frontend
    ↓
Backend Receives Request ✅
    ↓
Backend Sends Response ✅
    ↓
Browser Blocks Response ❌

That's why you sometimes see the API hit in your backend logs but still get a CORS error in the browser.


Why Doesn't Postman Show CORS Errors?

Because CORS is a browser security feature.

Tools like:

  • Postman
  • cURL
  • Insomnia

don't enforce browser security policies.

So the same request works perfectly there.


The Key Takeaway

Even though both applications are running on the same machine:

localhost:3000
localhost:5000

they are considered different origins because the ports are different.

The browser doesn't care that they're on the same laptop. It only cares about the origin.

That's why CORS exists, and that's why adding the correct Access-Control-Allow-Origin header fixes the issue.