简体   繁体   English

使用节点后端部署 create-react-app 的建议

[英]Recommendations on deploying create-react-app with node backend

I am trying to deploy my React app that uses a node.js express backend (running on a AWS EC2 instance).我正在尝试部署使用 node.js 快速后端(在 AWS EC2 实例上运行)的 React 应用程序。 I am having trouble deciding how to deploy my React App to make sure it can fetch from the backend.我在决定如何部署我的 React 应用程序以确保它可以从后端获取时遇到了麻烦。

  1. I can set my website example.com to direct to a AWS S3 static hosting that has my React app.我可以将我的网站 example.com 设置为指向具有我的 React 应用程序的 AWS S3 静态托管。 In this case, I would have to have to hard code my ec2 public ip eg fetch(" http://12.35.143.34/api/users/1 ") in the React code.在这种情况下,我将不得不在 React 代码中对我的 ec2 公共 ip 进行硬编码,例如 fetch(" http://12.35.143.34/api/users/1 ")。

  2. I can set my website example.com to direct to an ec2 instance with Nginx that will direct /api calls to the server and any other type of calls to the S3 bucket.我可以将我的网站 example.com 设置为使用 Nginx 定向到一个 ec2 实例,该实例会将 /api 调用定向到服务器,并将任何其他类型的调用定向到 S3 存储桶。

I read different posts about both ways so I am confused on which way is right.我阅读了关于这两种方式的不同帖子,所以我对哪种方式是正确的感到困惑。

Edit: Or is there a third option such as an api subdomain?编辑:或者是否有第三个选项,例如 api 子域?

My way :我的方式 :

1.deploy your node app to EC2 try to attach a DNS or just use the given one by aws - here 1.将您的节点应用程序部署到 EC2 尝试附加 DNS 或仅使用 aws 给定的 DNS - 此处

2.test your api using postman. 2.使用邮递员测试您的api。

3.change the api url endpoint value to the api DNS 3.将api url端点值更改为api DNS

4.deploy (upload) your dist/ (build directory) - here 4.deploy(上传)你的 dist/(构建目录)—— 这里

5.config the bucket to act as a static website host - here 5.配置存储桶作为静态网站主机 - 这里

6.(optional) try to configure the CloudFront to your bucket and get app dns - here 6.(可选)尝试将 CloudFront 配置到您的存储桶并获取应用 dns - 此处

There are many ways to run a NodeJS based application on AWS.有很多方法可以在 AWS 上运行基于 NodeJS 的应用程序。 The best one for you depends on your case, and how your app is developed.最适合您的方案取决于您的情况以及您的应用程序的开发方式。

  1. Use AWS Elastic Beanstalk使用AWS Elastic Beanstalk
  • It is a PaaS service that lets you run your application on top of AWS infrastructure, without you having to configure these services individually.它是一种 PaaS 服务,可让您在 AWS 基础设施之上运行您的应用程序,而无需单独配置这些服务。
  • Documentation 文档
  1. Use AWS Lambda + AWS API Gateway使用AWS Lambda + AWS API 网关
  1. EC2 using ELB and Auto Scaling Groups . EC2 使用ELBAuto Scaling 组
  • Regardless of the size of your application, you should run it with some level of high availability and load balancing.无论您的应用程序有多大,您都应该以某种程度的高可用性和负载平衡来运行它。
  • You shouldn't run your application on just one node;你不应该只在一个节点上运行你的应用程序; you should use an Auto Scaling Group.您应该使用 Auto Scaling 组。
  • You shouldn't publish your EC2 instances directly on the Internet;您不应直接在 Internet 上发布您的 EC2 实例; you should use an Application Load Balancer.您应该使用应用程序负载均衡器。
  • If your application uses a database, you should try to move it to RDS, and not run it by yourself.如果您的应用程序使用数据库,您应该尝试将其移动到 RDS,而不是自己运行。

And finally, to answer your question, I would recommend 3: you should use multiple CloudFront origins.最后,为了回答您的问题,我建议 3:您应该使用多个 CloudFront 源。 Us an S3 bucket origin to hold the static data, and an ELB origin for the dynamic, non-cacheable content.我们使用 S3 存储桶源来保存静态数据,以及用于动态、不可缓存内容的 ELB 源。 Check this post for more information on how to do it . 查看此帖子以获取有关如何操作的更多信息

If you own a domain, you can also create a certificate and serve your content through HTTPS, handled by CloudFront.如果您拥有域,您还可以创建证书并通过 CloudFront 处理的 HTTPS 提供您的内容。

I hope it helps.我希望它有帮助。

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

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