Skip to content

CORS error #57

Description

@MateeRixx

Adding this md file explaning what is cors erros and how to handle it , please checkout @abhiramiramadas

What is CORS?

Alt text

CORS stands for Cross-Origin Resource Sharing.

It is a browser security feature that controls which websites are allowed to request data from your server.

Why does CORS exist?

Imagine you log into your bank website (https://yourbank.com).
Later, you visit a hacker's website (https://hackersite.com).

Without protection, the hacker's site could secretly send requests to your bank (like "transfer money") using your logged-in session.
CORS prevents this by making sure only trusted websites can talk to your backend.


What is Origin?

Origin = Protocol + Domain + Port

Examples:

  • https://example.com
  • http://localhost:3000
  • https://api.myapp.com:8080

If two URLs have different origin, the browser treats them as cross-origin.


Where do you encounter CORS errors?

You usually see CORS errors when building full-stack applications:

  • Frontend running on http://localhost:5173 (Vite/React)
  • Backend running on http://localhost:3000 (Node.js/Express)

When your frontend tries to call the backend API using fetch() or axios, the browser blocks the request and shows an error like:

Access to fetch at 'http://localhost:3000/api/data' from origin 'http://localhost:5173' 
has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is very common during development.


How CORS Works (Step by Step)

  1. Frontend sends a request (e.g. fetch('http://localhost:3000/api/data'))
  2. Browser checks if the request is same-origin or cross-origin
  3. If cross-origin → Browser adds an Origin header (e.g. Origin: http://localhost:5173)
  4. Backend must respond with special headers:
    • Access-Control-Allow-Origin: http://localhost:5173 (or *)
    • Access-Control-Allow-Methods: GET, POST, PUT, DELETE
    • Access-Control-Allow-Headers: Content-Type, Authorization
  5. If backend allows it, browser lets the response reach your frontend.

If backend doesn't send these headers → CORS error


How to Fix CORS Errors

1. Best Way (Recommended for Development)

Use CORS middleware in your backend

For Node.js + Express:

npm install cors
// server.js
const express = require('express');
const cors = require('cors');

const app = express();

// Allow all origins (easy for development)
app.use(cors());

// OR allow specific origins (more secure)
app.use(cors({
  origin: ['http://localhost:5173', 'https://yourfrontend.com'],
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization']
}));

// Your routes...
app.get('/api/data', (req, res) => {
  res.json({ message: "Hello from backend!" });
});

2. For Production

Never use origin: '*' in production.

Instead, explicitly allow only your actual frontend domain:

app.use(cors({
  origin: 'https://yourproductiondomain.com',
  credentials: true   // if you use cookies or auth headers
}));

3. Other Quick Solutions (Not Recommended for long term)

  • Browser extensions like "CORS Unblock" (only for testing)
  • Proxy in frontend (Vite/React config) — good for development only

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions