node.jsreactjsnetlify

API from proxy not working after deploying on netlify


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?


Solution

  • 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