Node Js Application On Hosting Cloud
Let's first look at finished work. Click here
To demonstrate the whole procedure, I have created a subdomain nodereactapp.gauravadhikari.com where I will place all my node & react app files.
I'll use one of my Node React projects from GitHub. Here is the repo link if you want to check code.
Our React app resides in /client directory
Follow these simple steps to host node react app on web server
1. Go to your cPanel and search for Terminal
2. In Terminal, type cd your_project_directory, for me it is cd nodereactapp.gauravadhikari.com and then git clone https://github.com/gauravadhikari1997/MERN-product-app.git to clone the project
3. Now let's create script to run our node and react app Open package.json and add this script in scripts object "myScript": "concurrently --kill-others-on-fail \"npm run start\" \"cd client && npm install && npm run build\"",
4. Now open cPanel and search Node and click on Setup Node.js App
5. Now click on Create Application, this brings a form and we need to populate it with some obvious values like, where our project files are and Application URL, Startup file, etc
6. After that click on Run NPN Install
7. After that click Run JS Script, and click on myScript which you and I added before. On running this you will notice that react-scripts are not installed
To fix this issue, we need to go to Terminal again, move into client directory, and install scripts one by one.
8. Now Run myScript again and it will work fine.
9. Open your Application URL and Voila! Your app on the web, now pat yourself on the back
Thanks for reading! I hope you learned something useful.