简体   繁体   English

构建节点前端和后端并将其部署到 Google App Engine

[英]Structuring and deploying node frontend and backend to Google App Engine

I'm working on a React project with two node apps:我正在开发一个带有两个节点应用程序的 React 项目:

  • Client : Frontend code using React JS and Axios for making requests to my backend service. Client :前端代码使用 React JS 和 Axios 向我的后端服务发出请求。
  • API : Backend service using Mongoose to connect to MongoDB and Express for handling restful requests. API :后端服务使用 Mongoose 连接到 MongoDB 和 Express 以处理 restful 请求。

I had a couple questions:我有几个问题:

  • Is it a best practice to create separate node applications like this for frontend and backend?为前端和后端创建像这样的单独节点应用程序是最佳实践吗? I want to confirm this is a good setup.我想确认这是一个很好的设置。
  • I've had some difficulties in deploying this to Google App Engine and could use some pointers.我在将其部署到 Google App Engine 时遇到了一些困难,可以使用一些指针。

What I've currently done:我目前所做的:

  • Added an api.yaml to my API service subdirectory.在我的 API 服务子目录中添加了api.yaml
  • Deployed using gcloud app deploy client/app.yaml api/api.yaml which deploys two services (frontend and backend).使用gcloud app deploy client/app.yaml api/api.yaml部署,部署两个服务(前端和后端)。

Is this a good approach?这是一个好方法吗?

If so, the challenge I'm now facing is that my API service is deployed to a subdomain and requests from my frontend are failing.如果是这样,我现在面临的挑战是我的 API 服务部署到子域并且来自我的前端的请求失败。 Locally I am using the proxy value to proxy requests to my backend port on localhost, but I'm not sure how to modify my app to dynamically make backend requests to the correct subdomain when in the production environment (I also think CORS comes into play).在本地,我使用proxy值将请求代理到本地主机上的后端端口,但我不确定如何修改我的应用程序以在生产环境中动态地向正确的子域发出后端请求(我也认为 CORS 发挥作用)。

You need a dispatch.yaml file.您需要一个dispatch.yaml文件。 The dispatch.yaml allows you to override routing rules. dispatch.yaml 允许您覆盖路由规则。 You can use the dispatch.yaml to send incoming requests to a specific service (formerly known as modules) based on the path or hostname in the URL.您可以使用 dispatch.yaml 根据 URL 中的路径或主机名将传入请求发送到特定服务(以前称为模块)。

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

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