Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

983
Views
How to configure fetch base url on deployment in docker for react app?

I have a React app with fetches from an API in it. I have a dockerfile that works.

How do I configure the base url for all fetches in production?

In the end I will deploy my React app on Azure App Services. I used the tag proxy in package.json, but this is only for development. An Example fetch looks like this: (before "/evaluations" the base url has to be placed)

    fetch(`/evaluations?onlyactiveones=true`, this.credentials)
        .then(response => response.text())
        .then(data => {
            this.setState({ evaluations: data });
            console.log(data);
        });
10 months ago · Santiago Trujillo
2 answers
Answer question

0

Looks like you are making fetch calls to relative url. So by default it will make call to your domain as base url. For example in this case, http://yourdomain.com/evluations, but most likely you want to make call to http://someotherdomain.com/api/evaluations. In order to do that you need to configure reverse proxy in your azure web site (or any other hosting server)

In azure website you can configure reverse proxy as suggested in the following link: https://ppolyzos.com/2015/10/26/reverse-proxy-functionality-in-azure-web-sites/

10 months ago · Santiago Trujillo Report

0

I have now solved this problem. The things that I have done:

  1. Define a env variable for the base url, like: process.env.REACT_APP_API_PROXY
  2. Replace all fetches with this env variable. For the example above:

        fetch(`${process.env.REACT_APP_API_PROXY}/evaluations?onlyactiveones=true`, this.credentials)
        .then(response => response.text())
        .then(data => {
            this.setState({ evaluations: data });
            console.log(data);
        });
    
  3. Define the env variable as you like. The env variable has to be placed before building the react app, it can not be replaced at runtime.

10 months ago · Santiago Trujillo Report
Answer question
Find remote jobs