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

View all comments

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.