2

I am using api which is deployed in heroku to my react app. To fix the cors error, I add proxy in package.json as

"proxy":"https://mobtech.herokuapp.com"

While calling api , i do it like this

 const res = await axios.get('/api/user')

So, doing such works fine on development but not after deploying to netlify. I read official docs from netlify docs click but it doesnot lighten my mind. Can someone help me by giving example?

2
  • Proxy doesn't work in production
    – WebbH
    Jun 15, 2021 at 14:24
  • It works on heroku. So what could be solution?
    – Nick
    Jun 16, 2021 at 0:42

1 Answer 1

0

I maybe late but here how it is done. Netlify should have special file in your root directory called __redirect and you can define your proxy there like

/api/* https://yourapi.domain.com/:splat 200
/* /index.html 200           

Then you can call your api like this

const res = fetch('/api/user')   //equivalent to https://yourapi.domain.com/user

Hope this helped

2
  • Hey Saral, I know it's not place to ask, but can we connect? I have some questions related to netlify. Please ?
    – Agent K
    Jul 9, 2021 at 2:51
  • Netlify has very good support team and they reply every your question in a quick time. I am not in netlify support team. So you can consider asking there answers.netlify.com Jul 9, 2021 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.