0

I have a React JS/Spring Boot app where the React JS part accesses the Spring Boot server URL via a link to get a report.

The problem is that, first, the IP address is hardcoded in React, and second, I have to swap the IP address to localhost when testing.

Here's the code:

  download() {
    // fake server request, getting the file url as response
    setTimeout(() => {
      const response = {
//        file: "http://1.1.1.1:9080/api/export",
        file: "http://localhost:8080/api/export",
      };
      // server sent the url to the file!
      // now, let's download:
      window.open(response.file);
    }, 100);
  }

How do I fix this issue?

7
  • Parametrize it server-side before serving the website. Commented Jul 6, 2021 at 13:07
  • If using create-react-app, you have dotenv already available. If not: you can install it or using envvars in any case. See stackoverflow.com/questions/52896204/… Commented Jul 6, 2021 at 13:24
  • Does this answer your question? Using different API url for development and production with React and axios Commented Jul 6, 2021 at 13:25
  • @keul - if I'm reading the doc correctly, it means I would have to change a .env variable locally before compiling it for production. Is this correct? If so, it still means I'd have to remember to change it and recompile it before uploading it to the prod server, right? Commented Jul 6, 2021 at 15:04
  • @Alejandro - how would that work? You mean to create an API on the server that would check the environment and provide the IP address? Or have the server code read a file on the server that would contain the IP address? Commented Jul 6, 2021 at 15:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.