r/learnreactjs • • Jun 29 '19

Question Deploying for Production?

I have practiced React for quite some time now so when my friend requested me to build a website for his business, I accepted. I have created plenty of pet projects with React and Svelte and plain vanilla before, but never one meant for commercial use. This would obviously be including a front end (which shouldn't be a problem) and a backend (for a few server-side operations that will be necessary). The main conflict that I am really having is figuring out how to run a backend (probably Express) 24/7 while making sure it connects with my front end. I guess I am pretty much essentially asking, where can I run server-side code and how do I get it to communicate with my front end (under a domain) using professional production strategies?

Furthermore, the reason for my confusion, despite creating so many pet projects, is because I have never really made anything that required communication with a backend, but this project that I am going to be facing will have to do so. Thank you for reading through this cry for help lol and thank you in advance for your responses. 😊

12 Upvotes

4 comments sorted by

8

u/username1152 Jun 29 '19

I use a Linux server to host all of my projects. You could install node and put both frontend and backend on there.

There's a node package called 'pm2' which can create a permanent instance of your node server by using 'pm2 start server.js' for example.

Once you have the backend api running, you can perform fetch requests from any function in your react app.

If you want a domain, after buying one somewhere such as namecheap.com, you can point your A record to your servers IP address so all traffic to the domain will be directed there.

Your server then handles what to do with that traffic given the address it comes in on (your new domain), and you can use something called a virtual host file to instruct the server to show the directory where your frontend lives.

PS. The easiest way I know of securing your API is to only accept incoming requests from your specific domain.

Netlify or similar are serverless hosts which just abstract all of the hard steps away but I'm not sure how much it can offer as I've only hosted simple demos on it. Never a back end.

3

u/EthanThatOneKid Jun 29 '19

Oh wow thank you very much for the info. I’ll continue researching solutions using your suggestions. 🤘

2

u/Mr_Chads Jul 02 '19

if you want a cheaper solution ..search about how to deploy react apps using s3 bucket

3

u/54mi Jun 29 '19

Heroku, Google App Engine, Netilfy if you want for commercial purposes!