Home > Back-end >  Calling API in production and development in react js
Calling API in production and development in react js

Time:02-20

if the Url is localhost/8085

Then all api call will be made to localhost:9764/API

if the url is http://10.1xx.xx.xx:8085/Amp Then all api call using 10.1xx.xx.xx:9764/api...

Is This possible in React js?

CodePudding user response:

it works for me.

const url=""
if(process.env.NODE_EnV==="development"){
   url =localhost:8085
}else if(process.env.NODE_EnV==="production"){
   url = "http://10.1xx.xx.xx:8085/Amp "
}

CodePudding user response:

Assuming that your app was created with create-react-app, you can create two different .env files - .env.development and .env.production and create inside them variables with the same names. Therefore, if you run your app locally, the app will use variable values from .env.development and in production from .env.production.

//.env.development
REACT_APP_API_URL=ABC
//.env.production
REACT_APP_API_URL=DEF
//Component.tsx
const URL = process.env.REACT_APP_API_URL

CodePudding user response:

You can get the host name (window.location.host) and do anything from that (https://stackoverflow.com/a/54691801/7174186)

  • Related