[英]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 应用程序以确保它可以从后端获取时遇到了麻烦。
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 ")。
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.
最适合您的方案取决于您的情况以及您的应用程序的开发方式。
express
, it is simple to migrate it to AWS Lambda.express
,将其迁移到 AWS Lambda 很简单。 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.