How To Fix CORS Issue Permanently Right Now (2022)
Tags: javascript, nodejs, webdev, beginners
Published: 2022-05-17 17:13:51

---
canonical_url: https://softauthor.com/how-to-fix-cors-issue-permanently-right-now/
---

{% youtube VEed4m-rjAw %}

If you want to learn how to fix the CORS issue permanently, no matter what type of web app you’re building, you’ve come to the right place!

###Front-end Only JavaScript App (React, Vue or Angular)

Let’s say you want to get some data from an external server API, such as Google Maps API, from your client side app like the code below.

```JavaScript
fetch("https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJryijc9s0K4gRG9aU7SDTXdA&key=[YOUR_API_KEY]")
.then(response => {
   console.log(response);
})
```

If you run the above code, you will get the CORS Error.

![alt text](https://softauthor.com/wp-content/uploads/2022/05/how-to-fix-cors-error-message-1-1024x156.png)

>If you want to know the what, why and how about CORS, [check this out](https://softauthor.com/cors-error-solution-in-a-nutshell/).

To quickly fix it, use one of the public CORS proxy servers.

I use Heroku CORS proxy server in this example.

Append the proxy server to your API URL.

```JavaScript
https://cors-anywhere.herokuapp.com
```

You may get the 403 forbidden error even after adding the Heroku CORS proxy URL.

![alt text](https://softauthor.com/wp-content/uploads/2022/05/how-to-fix-cors-heroku-403-forbidden-error-1024x318.png)

To fix this, you need to request temporary access to the Heroku Proxy Server by going to the below URL.

```JavaScript
https://cors-anywhere.herokuapp.com/corsdemo
```

###Node.js Server-Side Only Web App

If you’re making an HTTP request to an external server API (in my case I use Google Maps API) from a server rather than client, you won’t have any problem dealing with CORS error because its just purely how the browser handles an HTTP request.

Check out [Up and Running With NodeJS Express App In A Minute (2022)](https://softauthor.com/create-nodejs-express-app-faster/)

```JavaScript
const { default: axios } = require('axios');
const express = require('express');
const app = express();

app.get("/", (req, res) => {
    axios.get('https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJryijc9s0K4gRG9aU7SDTXdA&key=[YOUR_API_KEY]')
        .then(function (response) {
            res.send(response.data);
        })
        .catch(function (error) {
            console.log(error);
        });
});

app.listen(3000);
```

###Client-Server App

Let’s talk about how to fix the CORS issue when you’re building a web app that separates the front-end app from the server-side api.

Keep the [Google Maps API](https://softauthor.com/category/google-maps-api/) request in the server side code which is running on the localhost:3000.

Then, change the URL of the fetch request on the client from the [Google Maps API](https://softauthor.com/category/google-maps-api/) URL to localhost:3000.

```JavaScript
fetch("http://localhost:3000/")
.then(response => {
   console.log(response);
})
```

And it’ll throw the same CORS error like before.

The good news is the CORS error is coming from my own server that is running on the localhost:3000 instead of a third party server like Google Maps API that I do not have any control over to change any code on the back end.

To fix that, you’ll need to add a response header called Access-Control-Allow-Origin on the server-side app passing the client side URL that you want to give access to.

In my case, I’m running my client side app on localhost:5501 or 127.0.0.1:5501

What that means is, no other client web app can consume your server to get external API data, in this case Google Maps API, which basically means you’ve created your own CORS proxy server instead of using a third party one like [Heroku](https://www.heroku.com/).

```JavaScript
app.get("/", (req, res) => {
     ...
     .then(function (response) {
        res.header("Access-Control-Allow-Origin", "http://localhost:5501"); // update to match the domain you will make the request from
        res.send(response.data);
      })
     ...
});
```

![alt text](https://softauthor.com/wp-content/uploads/2022/05/how-to-fix-cors-client-server-api-fixed.png)

This way, you’ve permanently fixed the CORS error and you’ll never see them again.

###Deployment

You can use services like Firebase to deploy your client and server side app quickly for FREE if you’re not worried about custom domain.

For the front-end deployment, you can use [Firebase hosting](https://softauthor.com/deploy-vue-js-app-on-firebase-hosting/).

For the Node.js app deployment, you can move your code to [Firebase Cloud Function](https://firebase.google.com/docs/functions/http-events) and deploy it .

If you’ve any question or concerns, please let me know in the comment section below.

Thanks for reading.