How to host Node.js express RESTful API on cpanel
Tags: javascript, node, tutorial, react
Published: 2022-02-25 14:31:13

Supposing you’ve developed your awesome node.js express RESTful API, you have many alternatives of hosting platforms such as Heroku, digital ocean, aws, gcp e.t.c which is perfectly fine. But did you know that you can still deploy your node.js express RESTful API on cpanel which will still behave like it’s hosted on a cloud server? Well, in this article I’m going to show you step by step tutorial on how to deploy your node.js express RESTful API on cpanel.

## Video Tutorial

_If you are the visual type like me, go ahead and watch the video below.
Please don't forget to like, share, comment, subscribe and turn on notification as that will give me the motivation to do more awesome videos and tutorials like this.

{% youtube OXTPJqTihhQ %}

## Things to note before hosting your react.js app

- Ensure your domain is linked to your hosting account.

- If main domain is already in use, create a subdomain dedicated for this project.

- In this article we will be using https://supablogapi.supatechie.ga but go ahead and replace it with your domain or subdomain.

Before we begin, ensure that you change your client app (react.js app) ajax requests base URL to point to URL you will be hosting your node.js app on. For instance, if you used **axios** in your client app connected to this server to make requests, change all your requests base URL to point to this remote  server URL. Example; assuming your server is hosted on **http://localhost:5000** locally and your remote server URL is **http://supablogapi.supatechie.ga**, make the changes below;
`const result = axios.get(‘http://localhost:5000/api/posts’)`
// change it to
`const result = axios.get(‘http://supablogapi.supatechie.ga/api/posts’)`

Do this for all your client side requests.

After you have done the above changes, proceed to host your server API.

- Under the server directory run the following command . This will install cors & dotenv dependency for you. Cors stand for cross-origin resource sharing and it allows client server communication. dotenv package will enable us to read environment variables from **.env** file.

```
npm install cors dotenv
```
- Go to your server main file be it **index.js** or **server.js** or **app.js** and require or import dotenv & cors dependency then add it as a middleware and provide the URL of your client app as shown below.

```
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const app = express();

app.use(cors({ origin: process.env.REMOTE_CLIENT_APP, credentials: true }));

```

-   Create **.env** file or if it already exist, paste the following code and save.

```
REMOTE_CLIENT_APP = https://supablog.supatechie.ga
```

-	Once you have done, go back to your desktop and locate the server directory and zip all the files in it excluding node_modules, .git, package.json-lock. 

-	Login to your cpanel and navigate to Files explorer. We are assuming our API to be hosted under a subdomain known as http://supablogapi.supatechie.ga. This could be your own subdomain or domain. If you don’t have one, go ahead and create a subdomain with any name of your choice. If you have just created a subdomain, it should create a subdirectory under your Files Manager. 

-  Within the cpanel homepage and scroll down to the **software section** and click on **setup node.js app** and it should take you to a page to set up your app.


![Cpanel Files Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8mvf8zugsmua4l1s20s.png)

i.	Once you’re on the page, click on **Create  Application** button. 

![Node app page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ry2vei5wpz5qs8aezrll.png)

ii.	On the same page, change your node.js version to match your development version. 

![Node version page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adcm0x8f9r5mndrp0z06.png)

iii.	Change node.js mode from development to production.
![Node Environment page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jizoa9u4rcbtyflyzz2a.png)
iv.	Enter the app document directory of which in this case it’s the name of the server subdomain or domain directory found under Files manager. E.g  **supablogapi.supatechie.ga**  if your server URL is a subdomain


![Node app document](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjb09she3liu350u9q7x.png)

v.	Enter select the app domain  or  subdomain URL. E.g  **https://supablogapi.supatechie.ga**  if your server URL is a subdomain


![Node Domain Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kwice05hpxk786focri.png)

vi.	Enter the server startup file which is the name of your main node.js app file e.g index.js or server.js or app.js in your dev environment.


![Node startup file ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r40kzvdj8x5niqbkjkts.png)


vii.	Next click on create app and your app should be up and running assuming no error occurred.

![Node create app ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzqh9egbsem5y4h5i5ot.png)

viii.	To check if it’s running smoothly, click on the open button to see a message like “it works.Node 14.14”

ix.	Click on the button stop app to stop the app from running

**LEAVE THE NODE.JS SERVER TAB OPEN & OPEN A NEW TAB**

-	Go back to cpanel homepage and scroll down to the Files section and click on **File Manager **.

![Cpanel homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67eb14008wsoa0cvvnw4.png)

-	Navigate back to your Files Manager under the server directory. 


![Cpanel files ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj63rprqudn3zw2yh2hq.png)

-      If you’re within the directory, click on upload at the top navigation bar and this should take you to a new page to upload your files.

![Files upload ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnohgz10extnhqjzdzab.png)
-	 This should take you to an upload page. Click on the select file and it should open your system’s file explorer for you. Navigate to your server directory and select the **server.zip** file


![Files upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7v6qx3z617l9yonn3r3o.png)

-	Once the zip file is uploaded, click on the link below to go back to your cpanel File Manager.

-	You will locate the **server.zip** over there, select the file and click on extract file on the top right. Your files will be extracted successfully


**Right now, we need to install the dependencies.**

-	 Switch back to your node.js app tab and refresh the page and at the bottom, you will notice button **run npm install** Click on it and wait for it to install your dependencies.  


![Run npm install](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aow6wwodyedt89xkcupx.png)


-	 Once it is installed, you should see the message below;


![Sucess message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlvti30lb4r2gb2kjlrr.png)

-      Click on start app.


![start app ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/336qr50p7w4sx84fyxiy.png)

-	 Open a new browser tab and try to search for one of your get endpoints. E.g **http://supablogapi.supatechie.ga/api/posts** and this will return a successful response displayed in the browser

-	 To test if your client app is connected with your server, open a new browser tab and type your domain name of your client app(assuming it is hosted or check the article below to host your client app as well)  and the site should be opened successfully getting data from your server.


If you have made it so far, congratulations. 

###### Credits

Image background is designed by [Business vector created by fullvector - www.freepik.com](https://www.freepik.com/vectors/business) 

---

If you want to watch in-depth tutorial on how to develop your react.js and node.js app completely from the scratch and host it on cPanel, I have created a YouTube playlist for this. Please watch the video below.

{% youtube ZO-DPIhZ2Fo %}
If you have any question, ask me below in the comment section or on my YouTube channel.

_Also, don’t forget to like, share, subscribe to my YouTube channel and turn on notifications. Thanks_