DEV Community

Mujtaba Hussain
Mujtaba Hussain

Posted on

Resolving CORS error

If you've ever worked on a backend project and then tried to connect it to your frontend, you've probably run into CORS errors. Bam! Suddenly, things don't work.

So, what's the deal with CORS?

What is CORS?

CORS stands for Cross-Origin Resource Sharing. It's a security feature built into browsers that controls access to resources (like APIs) from a different domain than the one serving your web page.

Why do CORS Errors Occur?

CORS errors pop up when your web app tries to make a request to a different domain without the right permissions. It's a security measure to stop shady sites from making unauthorized requests to other domains.

But you might be thinking, "I'm just developing on my local machine. How can the domain be different?" Good question! Even on your local setup, if you look closely, you'll notice your frontend and backend URLs are slightly different. For example, they might be http://localhost:3000/ and http://localhost:4000/. See the difference? It's in the port numbers: 3000 and 4000. This tiny change means they're considered different domains, and boom - CORS error.

You've probably seen error messages like these:

  1. "Access to fetch at [URL] from origin [Origin] has been blocked by CORS policy"
  2. "No 'Access-Control-Allow-Origin' header is present on the requested resource"

Now that you know what these errors are, how do you fix them?
It's simple...

First things first, if you're using Node.js with Express (which is super common), you'll want to use the CORS middleware. It's like a magic wand for CORS issues.

Step 1: Install the CORS package
Open up your terminal, navigate to your project folder, and type:

npm install cors
Enter fullscreen mode Exit fullscreen mode

This'll add the CORS package to your project.

Step 2: Use the middleware
Now, crack open your main server file (maybe it's called app.js or server.js) and add these lines:

const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());
Enter fullscreen mode Exit fullscreen mode

That's it! This setup will allow all origins, which is fine for development but a bit too open for production.

Want to be more specific? No problem. You can set which origins are allowed:

app.use(cors({
  origin: 'http://localhost:3000'
}));
Enter fullscreen mode Exit fullscreen mode

Replace 'http://localhost:3000' with your frontend's URL.

Sometimes, you might need to handle OPTIONS requests explicitly. If that's the case, just add this line:

app.options('*', cors());
Enter fullscreen mode Exit fullscreen mode

Now, if you're not using Express, don't sweat it. You can manually set CORS headers like this:

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});
Enter fullscreen mode Exit fullscreen mode

Remember, using '*' for the origin in production is like leaving your front door wide open. In the real world, you'll want to specify exactly which origins are allowed.

That's pretty much it for the backend solution! Give it a shot, and those CORS errors should disappear like magic. If you're still having trouble, double-check your frontend code or server configuration. Sometimes the issue might be hiding somewhere else.

Happy coding, and may your CORS errors be few and far between!

Top comments (0)