Sitemap

Handling proxy in React

2 min readMar 16, 2020

--

In this article, let’s find the easy way of setting a proxy for React front end projects.

Let’s assume we have a backend server running on https://localhost:5001.

I assume you already have a React project up and running, otherwise, we have to create a new one.

npx create-react-app ding-app
cd ding-app
npm start

Inside the front end project, we want to make the request calls like this (We don’t have to add any base url at this case):

axios.get(`/api/WeatherForecast`)

.then(res => {

console.log(res.data);

setData(res.data);

setLoading(false);

}

Get Jet丁Neat Coding’s stories in your inbox

Join Medium for free to get updates from this writer.

).catch((error) => {

// handle error

console.log(error);

});

Now, let’s prepare the proxy settings for the above purpose:

Inside package.json, let’s add the following line:

We assume we have a server running at 5001 port locally.

“proxy”: “https://localhost:5001",

After this setting is ready, you can restart your react project to make the api call. It will work now.

Press enter or click to view image in full size

That’s all for this session.

See you in the next session. Have fun.

--

--