React proxy not conecting to api url

Viewed 1973

when i am building a social media website, but i have run into a big problem, my api server runs on localhost:6000, and my react app runs on the default of localhost:3000, i have setup my proxy in the react app's package.json to the api url (localhost:6000), but when i make an api call using axios for example

const response = await axios.get("/api/users", body, config);
console.log(response.data);

the front end makes the api call to the localhost:300 url not my api url, please what can i do to fix this problem it has been bugging me for the past 2 days now lol.

3 Answers

try setting "proxy": "127.0.0.1:6000" in your package.json file for your client.

I have 3 suggestions you could try and follow

axios.get('http://localhost:6000/api/users')

or have you tried adding this to the request?

headers: {
    "accepts":"application/json"
}

and finally you could try and use this package

const { createProxyMiddleware } = require('http-proxy-middleware');

const response = await axios.get("http://localhost:6000/api/users", body, config);
console.log(response.data);

Should work, or am I missing something?

Related