简体   繁体   中英

How to have App Engine front-end service use API from different App Engine service

I have two App Engine services, one for my React front-end and the other for my Express back-end. Not that while these two things are a part of the same app, they are different services and thus have different URLs which they are accessed through.

All requests in my React app are setup to use the full url of the back-ends endpoints, for example:

Axios.get(`https://backend.appspot.com/api/v1/users/get/${userId}`)

However, this means that in I can't develop my backend locally because the React app is always pointing to the App Engine service.

What I am trying to accomplish is having something like you would if your back-end and front-end were on the same server and you'd just use the React proxy setting, allowing you to make request to your endpoints like this:

Axios.get(`/api/v1/users/get/${userId}`)

I have tried manually setting up a proxy, although now I'm wondering if that's the wrong approach entirely.

Any suggestions?

Note: Please let me know if this question needs further explanation

I do not think it is possible to achieve something exactly like developing on your local environment, but you always have the option of configuring the routing rules and the URLs as you wish.

In order to create your own routing rules you can use the dispatch.yaml file to define them in it. This way you can choose exactly how your requests will be routed .

As for your concern about the development process, you need to do some configuration in your code. You basically, need to make your app smart enough to know where is it running: on local or on app engine.

In order to do that I would recommend you to define in your app.yaml an env variable like this:

env_variables:
IS_APPENGINE: 'true'

Then in your application you can check where it is running and set your URLs paths:

if(Platform.environment['IS_APPENGINE'] == 'true') {
URL1 = .....
URL2 = .....}  else{
URL1 = localhost/page1..
URL2 = localhost/page2..
}

There is a discussion about this topic in this post , as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM