Simple CORS Guide: How to Fix Cross-Origin Errors in Your Web Projects


What Is CORS? Let’s Break It Down!
You’re building your cool new web app with React on the frontend and Node.js on the backend. You’ve got everything set up, but when you try to fetch data from your backend, the browser goes “Nope. Not Allowed.”
What happened? You probably ran into CORS—Cross-Origin Resource Sharing. It’s a security feature that helps protect users from malicious websites.
So, What’s the Big Deal with CORS?
Imagine you’re trying to send a message to a friend, but the person on the other end is super cautious. They want to make sure you’re not a random stranger. So, they check if your request is coming from a trusted place. If you’re not on their list, they block your message.
CORS works the same way. When your frontend (React) asks your backend (Node.js) for data, the browser checks if the server says it’s okay for that request to come through. If the server doesn’t give the green light, the browser blocks it.
Why Does Postman Work, but the Browser Doesn’t?
Here’s the twist, When you test the same request in Postman, it works just fine. But when you do the same thing in the browser, you get blocked. Why?
Simple: Postman doesn’t care about CORS. It’s just a tool for testing APIs. Browsers, though, are much more careful and will block requests if they don’t pass the CORS check.
Let’s Break It Down: A Real-World Example
Think of CORS like this, You’re sending a letter to a friend who lives in a different city. To make sure it reaches the right person, you need to put the correct address on the envelope. If the address doesn’t match, the post office will stop it and send it back.
In this example:
Your frontend is like the letter you’re sending.
Your backend is like the friend who’s waiting for the letter.
The browser is like the post office checking if everything matches and is safe.
If something doesn’t match (like the server not sending the right CORS headers), the browser will stop the request from going through.
How Do You Fix CORS?
Now that you know what’s going on, how do you fix it?
You need to tell your backend to allow requests from your frontend. This is usually done by adding something called a CORS middleware on the server. If you’re using Node.js, it’s super easy with the cors
package. Here’s how you set it up:-
javascriptCopyEditapp.use(cors());
By using this, you’re telling your server, “It’s okay for my frontend to ask for data.” The magic happens when the server sends the right headers, like:-
httpCopyEditAccess-Control-Allow-Origin: *
This header is saying, “Yes, this frontend is allowed to get data from me.”
But, don’t use this open setting (*
) in production. It’s better to be specific and only allow trusted domains.
What About Those Weird OPTIONS Requests?
When you make a request from your frontend, sometimes the browser sends a preflight OPTIONS request first. It’s like the browser asking the server, “Hey, can I send this request?” The server then responds with the appropriate CORS headers, saying, “Yes, it’s okay.”
If the server doesn’t respond with the right headers, the browser blocks the request.
Summing It Up: CORS in Simple Terms
CORS is a security feature built into the browser to protect users from unsafe requests.
It checks if the frontend is allowed to talk to the backend.
Postman doesn’t care about CORS, but the browser does.
If you get the “Not Allowed” error, it means the server didn’t send the right CORS headers.
To fix it, add the CORS middleware to your server, and make sure it’s sending the right headers so the browser will let the requests go through.
Subscribe to my newsletter
Read articles from Habibur Rahman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Habibur Rahman
Habibur Rahman
Hi! I’m an aspiring Full-Stack Web Developer currently pursuing my Bachelor of Computer Applications (LPU). With a solid foundation in C++, Data Structures & Algorithms, and a growing skill set in web technologies.