r/learnreactjs • • May 25 '20

Question How to deploy create-react-app with express?

Hello! I have two folders. In one folder, there is create-react-app having all my frontend and in another folder I have my express server with database connectivity i.e. backend. I want to deploy this to vps or hosting service? So should I keep my code like this in two different folders and run the two apps on vps or should I use nextJs so that I can keep all code into single file?

9 Upvotes

4 comments sorted by

5

u/m-sterspace May 25 '20

When it comes to folder structure, what you have is good, keep them the way they are, in separate folders.

When it comes to your front end, before hosting it, the first thing you'll have to do is build a production ready version of your site. If you used CRA, you can simply run yarn build in your frontend folder and it will create a build folder with a static copy of your site.

When it comes to hosting, you have two options, host them together as one server, or host them separately. If you host them together, you need to find a service to host your backend server and you need to add a route to your express server to serve up that build folder from your frontend whenever someone goes to the / route.

Alternatively, if you host them separately, you won't need to add an extra route to your express server, but you will need to configure your front end to reach out to whatever url you're hosting your backend at.

3

u/prodcoder May 25 '20

I think the easiest way would be to host it on different subdomains. I.e. you host your frontend application on mydomain.comand you host your express server on a subdomain like api.mydomain.com.

You could also host your frontend application under app.mydomain.com if you are using something like wordpress for your landing page under mydomain.com

2

u/MassRain May 25 '20

Basically you are asking microservices vs monolith structure i guess?

I recommend you to use seperate folders and projects. If your app grows you can even use 2 seperate vps's. I would use nginx to create different subdomains and folders on your server machine. Afterwards you can use subdomains(api.test.com) or same domains (test.com/api). You dont have to put them to same folder to create same domain linking. You can do it easily with nginx. And for express app you might need to do reverse proxy on nginx and process manager(like pm2) to keep express alive.

2

u/O-juice89 May 25 '20

If you’re familiar with AWS Amplify, you can load up your whole git repository, and then it’s a matter of setting some build time scripts which tell Amplify which path your front end and back end are in and it’ll do the rest.

Can be a bit to digest at first but it’s a breeze once you understand it :)