简体   繁体   English

如何让 App Engine 前端服务使用来自不同 App Engine 服务的 API

[英]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.我有两个 App Engine 服务,一个用于我的 React 前端,另一个用于我的 Express 后端。 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.并不是说虽然这两个东西是同一个应用程序的一部分,但它们是不同的服务,因此具有不同的 URL,可以通过这些 URL 访问它们。

All requests in my React app are setup to use the full url of the back-ends endpoints, for example:我的 React 应用程序中的所有请求都设置为使用后端端点的完整 url,例如:

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.但是,这意味着我无法在本地开发后端,因为 React 应用程序始终指向 App Engine 服务。

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:我想要完成的是,如果您的后端和前端在同一台服务器上,并且您只需使用 React 代理设置,您就会拥有类似的东西,允许您像这样向您的端点发出请求:

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.我认为不可能实现与在本地环境中开发完全一样的东西,但您始终可以根据需要配置路由规则和 URL。

In order to create your own routing rules you can use the dispatch.yaml file to define them in it.为了创建自己的路由规则,您可以使用dispatch.yaml 文件在其中定义它们。 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:为此,我建议您在app.yaml中定义一个像这样的环境变量:

env_variables:
IS_APPENGINE: 'true'

Then in your application you can check where it is running and set your URLs paths:然后在您的应用程序中,您可以检查它的运行位置并设置您的 URL 路径:

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.在这篇文章中也有关于这个话题的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从反应前端应用程序打开新选项卡 - How to open a new tab from a react front-end app 如何在Google App Engine中构建API和前端应用程序? - how should I architecture an API and front app in Google App Engine? 在Node.js中使用Oimo.js物理引擎(Oimo.js是前端引擎) - Use Oimo.js Physics Engine in Node.js (Oimo.js is front-end engine) 在Google App Engine上运行Bottle(后端)+ Nodejs(前端) - Running Bottle (back end) + Nodejs (front end) on Google App Engine 如何在 Firebase 数据更改上触发 App Engine 中的服务 - How to trigger a service in App Engine on Firebase data change 如何仅将受影响的 nx 服务部署到 GCP APP Engine - How to deploy only an affected nx service to GCP APP Engine 如何在前端应用程序和后端服务器之间共享Cookie? - How to share cookie between front-end app and backend server? App Engine上的React.js作为服务查询在同一项目中作为另一服务运行的API - React.js on App Engine as a service querying an API running as another service on same project 我是否需要在使用 Node.js 创建后端服务和使用 ejs 创建带有视图引擎的前端部分的项目中使用 Babel? - Do I need to use Babel in project which created backend service with Node.js and front end part with view engine with ejs? API-应该在前端或后端应用程序中进行汇总吗? - API - Should aggregation be done in the front-end or back-end app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM