S3 Amazon Static Website with React?

故事扮演 提交于 2021-02-07 06:25:26

问题


I built a website using ReactJs, and to see the website, I generally do npm start and go to localhost:3000 via a browser.

I now want to host this website on S3, but without an EC2 instance. My understanding is that npm is a process, so it is server-side, and therefore, I would need to purchase compute to actually deploy my website.

I found this tutorial that does not mention paying for EC2 instance compute time: https://www.fullstackreact.com/articles/deploying-a-react-app-to-s3/

However, they still use NPM which makes me confused.

My question is: is it possible to use React if I were to only use Static S3 Website, without compute, and if so - how do I bypass NPM process?

React - component in seperate script does not work

In the above post, user tried to make a hello-world app in react, but all of the answers point them in the direction of making a server serve the content. I thought react is a front-end thing and can run without server-side processes. Is this true? Can someone explain why node.js is necessary or is paired with react, and whether is is possible to use react on s3 without ec2 compute?


回答1:


It's possible to host a static react site entirely on s3. In this case, you would use node/npm only as build tools and to run your development server (localhost:3000).

npm would download your dependencies and you'd use node or gulp or webpack to build the assets into static files.

Then you would upload the files to s3 where it would serve the static files.

If you have some backend node code, then you would need to use ec2 or some other type of host. But if it's entirely static javascript, then there's no need for a node server.

Here are some links that might help explain in more detail:

  • https://medium.com/@omgwtfmarc/deploying-create-react-app-to-s3-or-cloudfront-48dae4ce0af
  • https://www.fullstackreact.com/articles/deploying-a-react-app-to-s3/



回答2:


Try Gatsby! Here: https://www.gatsbyjs.org/

From the Github page: "Blazing fast static site generator for React"

Once you've generated your static pages, you can deploy on S3, Github pages...the choice is yours!




回答3:


You can skip EC2 for your case. Here is why:

1) S3 Bucket + CloudFront (CDN) is really fast for static files serving. The React minified app is a group of static files which works best here. For the build of those minified files, I recommend using a CI/CD process or build them locally and just upload them to S3.

2) EC2 requires more work to setup, it consumes resources, it is not necessary for static files or React (unless you are using ReactDOMServer for dynamic content serving), and Node.js is not recommended for static files (Node.js get's blocked since it is single threaded so it is a best practice to keep static files away from it).

Here is a good article on the topic using Angular as an example: https://www.quora.com/Should-I-use-AWS-EC2-to-host-an-Angular-web-app-or-AWS-S3



来源:https://stackoverflow.com/questions/45989428/s3-amazon-static-website-with-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!