Ways4eu WordPress.com Blog

SPA View of ways4eu.wordpress.com

How to handle backend API request for Create-react-app build and ran using “serve-s build”? via /r/reactjs

on Mon May 11 2020

I have create-react-app bootstrapped application which I build and then serve it to the static server using serve-s build. The React app is running on http://locahost:3000.

This app talks to my backend REST API(using java) which is running on http://locahost:8080.

Inside my React application, I have set axios.default.baseURL=http://localhost:8085/api.

Everything is fine until the application is running on the localhost on my system. The React app talks to http://localhost:8085/api/xyz for CRUD operations and everything works great.

Now I have to deploy the project on AWS EC2 instance. The MySQL and REST API got deployed and rest API there is working on the same port as http://locahost:8080. I have Apache server configured which sends the hostname(www.myxyzwebsite.com) to the http://locahost:3000 on the server. Everything is fine till now. The home page is visible on the browser.

Now, when the React app tries to communicate to the REST API from the browser, it is sending the request to the http://locahost:8080. Obviously browser is looking for some service running on port 8080 on my system and it couldn’t find. Ideally, the request should go to the server with my hostname(www.myxyzwebsite.com/api/users/puspen).

NOTE: Please note that this is not a server-side-rendered application.

I have no idea how to handle this scenario. This is my first web project, please guide.

from How to handle backend API request for Create-react-app build and ran using “serve-s build”? https://ift.tt/2xUYxWS