简体   繁体   English

将 ReactJs 前端 + NodeJs 后端部署到 AWS

[英]Deploying ReactJs frontend + NodeJs backend to AWS

I have a NodeJs/React web app.我有一个 NodeJs/React web 应用程序。 I use to deploy to one EC2 instance.我用来部署到一个 EC2 实例。 Is this the correct approach?这是正确的方法吗?

I've seen online that one option is to put the frontend in S3 with CloudFront and then the backend in an EC2 instance.我在网上看到一种选择是使用 CloudFront 将前端放在 S3 中,然后将后端放在 EC2 实例中。 In this case does the NodeJs server serve the static files from that bucket?在这种情况下,NodeJs 服务器是否提供该存储桶中的 static 个文件? Or how would it work?或者它会如何运作?

Hosting dynamic part of React apps on Compute (I'll prefer ECS+Fargate over EC2) and static on S3 + CloudFront is an efficient way to use AWS Managed services capabilities.在 Compute 上托管 React 应用程序的动态部分(我更喜欢 ECS+Fargate 而不是 EC2)和在 S3 + CloudFront 上托管 static 是使用 AWS 托管服务功能的有效方式。 Your architecture will be something like this:您的架构将是这样的:

client -> CloudFront (cached) -> S3 (static resources)
client -> CloudFront (NOT cached) -> LB -> Compute (EC2 / Fargate / Lambda)

You can manage different paths or routes via CloudFront Behaviour functionality.您可以通过 CloudFront 行为功能管理不同的路径或路由。 So root (/index.html) points to your S3.所以 root (/index.html) 指向您的 S3。 and APIs (/api) can point to the Load Balancer for dynamic content. API (/api) 可以指向动态内容的负载均衡器。

The bucket works like a static web hosting.该存储桶的工作方式类似于 static web 托管。

Since React it's client rendering, and not server rendering, it's a complete different app than your node backend.由于 React 是客户端渲染,而不是服务器渲染,因此它是一个与节点后端完全不同的应用程序。

The client will recieve the static files from CloudFront/S3, and that files (HTML, JavaScript rendered in the client's machine), will communicate with your API hosted in EC2.客户端将从 CloudFront/S3 接收 static 文件,并且该文件(在客户端计算机中呈现的 HTML,JavaScript)将与您在 EC2 中托管的 API 进行通信。

You can host both, frontend and backend in EC2.您可以在 EC2 中托管前端和后端。

Or frontend in S3 or CludFront and the backend in EC2.或 S3 或 CludFront 中的前端和 EC2 中的后端。

It's cheaper if you host your stuff separately, since EC2 has dinamic costs.如果您单独托管您的东西会更便宜,因为 EC2 具有动态成本。

Now, if you use a server rendering framework like NextJS or Gatsby, the "static files" and the backend are the same thing, the backend render the static files and send them to the client, in that case you need to host everything in EC2.现在,如果你使用像 NextJS 或 Gatsby 这样的服务器渲染框架,“静态文件”和后端是一回事,后端渲染 static 文件并将它们发送到客户端,在这种情况下你需要在 EC2 中托管所有内容.

Usually is better to host everything in the same server for practicity, specially when there are not dynamic costs.为了实用起见,通常最好将所有内容都托管在同一台服务器上,特别是在没有动态成本的情况下。

You can opt for another VPS hosting for your server, it's not related to your question, but I reccomend Hetzner , it's cheaper and better than EC2.您可以为您的服务器选择另一个 VPS 托管,这与您的问题无关,但我推荐Hetzner ,它比 EC2 更便宜也更好。

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

相关问题 在 firebase 上部署 react 前端和 express/nodejs 后端 - Deploying react frontend & express/nodejs backend on firebase AWS ECS 前端和后端通信 - AWS ECS Frontend & Backend Communication 使用 Google Cloud App Engine 将前端和后端部署为两个独立的应用程序 - Deploying FrontEnd and BackEnd as two separate applications with Google Cloud App Engine AWS Angular 前端和 Spring 引导后端 SSL 问题 - AWS Angular Frontend and Spring boot Backend SSL issues Amazon AWS Amplify NestJs - 托管自己的前端和后端 - Amazon AWS Amplify NestJs - host own frontend and backend AWS NodeJS 无服务器 - 部署项目时出现 Zip 错误 - AWS NodeJS Serverless - Zip Error When Deploying Project 将 reactjs 应用程序部署到 aws amplify 会导致白屏并出现错误“Uncaught SyntaxError: Unexpected token '<'” - Deploying reactjs app to aws amplify results in white screen with error "Uncaught SyntaxError: Unexpected token '<'" 在 firebase 上托管我的后端和前端 - hosting my backend and frontend on firebase Reactjs 后端连接问题 - Reactjs backend connection issue 将 nodejs 和 mongoDB 部署到 azure - deploying nodejs and mongoDB to azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM