Reputation: 684
I am trying to deploy my React app that uses a node.js express backend (running on a AWS EC2 instance). I am having trouble deciding how to deploy my React App to make sure it can fetch from the backend.
I can set my website example.com to direct to a AWS S3 static hosting that has my React app. In this case, I would have to have to hard code my ec2 public ip e.g. fetch("http://12.35.143.34/api/users/1") in the React code.
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.
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?
Upvotes: -1
Views: 1469
Reputation: 2540
There are many ways to run a NodeJS based application on AWS. 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.And finally, to answer your question, I would recommend 3: you should use multiple CloudFront origins. Us an S3 bucket origin to hold the static data, and an ELB origin for the dynamic, non-cacheable content. 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.
I hope it helps.
Upvotes: 1
Reputation: 5202
My way :
1.deploy your node app to EC2 try to attach a DNS or just use the given one by aws - here
2.test your api using postman.
3.change the api url endpoint value to the api DNS
4.deploy (upload) your dist/ (build directory) - here
5.config the bucket to act as a static website host - here
6.(optional) try to configure the CloudFront to your bucket and get app dns - here
Upvotes: 1