2

I have an env file with such format:

REACT_APP_API_URL_API_LOGIN =  "http://x.x.x.x:1000/api/loginuser"
REACT_APP_API_URL_API_SIGNUP= "http://x.x.x.x:1000/reg/CheckRegister"
REACT_APP_API_URL_API_FETCH_IMAGE = "http://x.x.x.x:1000/api/userImage"
REACT_APP_API_URL_API_VERIFY_USER = "http://x.x.x.x:1000/reg/RegisterUser"

as you know for privacy reasons I changed IP:PORT

I want to separate my base url, the x.x.x.x:1000 part. because sometimes I need to change it for testing purposes.

I wish to have such thing:

REACT_APP_API_URL_API_BASE = x.x.x.x:1000
REACT_APP_API_URL_API_LOGIN =  "http://REACT_APP_API_URL_API_BASE/api/loginuser"

I thought about having 2 different .env files or defining a constant inside of React but it will make, editing codes harder. since I must search for my all,fetching functions all the time.

how can I use my base url inside of the same .evn file.

2
  • You usually store host name and endpoint path separately Commented Nov 21, 2022 at 9:43
  • @Konrad that means there's no way except creating another .env file isn't it? Commented Nov 21, 2022 at 9:44

1 Answer 1

1

in react .env file, you can use it like this

REACT_APP_API_URL_API_BASE = x.x.x.x:1000 
REACT_APP_API_URL_API_LOGIN =  http://$REACT_APP_API_URL_API_BASE/api/loginuser
Sign up to request clarification or add additional context in comments.

5 Comments

it gives me CORS error
that you need to enable from the backend
how can I do this? actually I only do front end but backend works with .Net Core.
backend needs to allow your host x.x.x.x:1000 to access their services. This needs to handle from the .net side
oh, you mean CORS problem. we don't have this problem. cross origin error only happens when I use this snippet. by manually typing it is working.

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.