Search code examples
reactjsherokudeploymentproduction-environment

Deploying React + API apps on two different hosts


I'm trying to figure out what are the best practices to deploy a React app that consumes an API on a different host in production.

Currently in DEV I have the following:

frontend - React app running on webpack server: http://localhost:3000/

backend - API (django-rest) running on: http://localhost:7000/

Right now I define the API url in package.json proxy attribute: "proxy": "http://localhost:7000/", and I make API calls using Axios to api/something/something/ etc.

Both apps are standalone with separate repos and I'd like to keep it that way (I don't want to merge both apps into a single codebase.)

My question:

What is the best way to configure React in production to consume the production URL?


Solution

  • Use process.env.NODE_ENV to find out the current environment and assign the proper url to your base url constant. In development, you will get process.env.NODE_ENV as "development" and in production, you will get process.env.NODE_ENV as "production".