0

Let's say I have a React app and want to connect locally to my local Tomcat server (for ultimately consuming REST endpoints from my React app). I have 2 questions;

  1. Is there a standard local workspace setup recommended by React to point to our localhost running backend services?
  2. Is there an easy/configurable setup, where I have both options e.g. switch from connecting to actual backend service TO say using mock endpoint responses on my local i.e. by a simple config change ?

Note: I am trying to avoid hardcoding any absolute URLs on my client-side i.e. In my client side code, I would just have the endpoint defined as "mycontext/my/endpoint" and say if my React app is running on say http://localhost, then it should automatically construct the full endpoint as http://localhost/mycontext/my/endpoint

1 Answer 1

1

You can define environment variables, which could include the address of the API server you'd like to use. Then you would simply change that variable any time you wish to hit a different API server (be it localhost or remote).

If you are using Create React App to bootstrap your setup, you can also use the proxy setting in your package.json.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks....Is there setup that can be done via ExpressJS (app.listen, app.use, app.get/post, etc) ?
I don't know much about Express. It's for Node, right? I think the standard answer for environment variables in Node is still dotenv.

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.