Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React API calls work in development and fail in production build

I'm trying to deploy a React project with a Spring Boot/Hibernate Restful CRUD API. I'm running the SQL database locally and running my React app in development mode (localhost:3000) allows for successful communication with my API.

However, when I built my app for production (localhost:5000), the API is unsuccessful. The API response is an error code 304. My best guess is that something is getting mixed up with the changing ports? It works as intended in development on port :3000 and fails when built on port :5000.

I've never deployed a React app before so thanks for any help!


API call

   const apiCall = () => {
    fileService
      .getUsers()
      .then((response) => {
        setUsers(response.data); //Incorrectly returns a 304 error
      })
      .catch(function (error) {
        console.log(error);
      });
  };

getUsers()

    getUsers() {
        return service.getRestClient().get("/api/getAllUsers");
    }

getRestClient()

getRestClient() {
    if (!this.serviceInstance) {
      this.serviceInstance = axios.create({
        baseURL: "http://localhost:5000/",
        timeout: 10000,
        headers: {
          "Content-Type": "application/json",
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
          "Access-Control-Allow-Headers": "Content-Type",
          "Access-Control-Allow-Credentials": true,
        },
      });
    }
    return this.serviceInstance;
  }
like image 554
Ben L Avatar asked Jan 30 '26 09:01

Ben L


1 Answers

Since posting, I learned that you can't use a proxy in a production version of React. "proxy:" in package.json only has an effect in development - not in production. Thanks for everyone's help.

Source: https://create-react-app.dev/docs/proxying-api-requests-in-development/

like image 116
Ben L Avatar answered Jan 31 '26 21:01

Ben L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!