Host your Node.js app on Shared hosting. Go beyond localhost.

Abhijit Srikanth
5 min readJan 13, 2020

--

Have you ever thought about hosting your Node.js projects online so, you can show off to the world? Well, you’re not alone. There came a point where I was comfortable with creating node servers locally for various projects, but I asked what next. The only way to go forward was to start hosting these local servers online [No more local].

The easiest option is to use Heroku. I began hosting all my node projects on Heroku for free. But here’s the deal; The URL will always be followed with “herokuapp”. It will take forever to load (of course, it's free duh!). And you can host a maximum of 5 apps on your heroku account. I was super excited to push all my websites online using heroku and git. But the loading time was a huge bummer and I had to go one step further. That’s when I looked into shared web hosting.

Here, I’ll be going through a detailed tutorial of hosting your Node.js app, with your own domain and with shared web-hosting. This involves shelling some bucks on buying your domain (GoDaddy or other Namecheap.com) + Buying a web hosting (again, my preference here was A2 hosting)

First things first, I hope you have an idea about Node.js and have your locally hosted node webserver ready. If not, just a heads up, I’m not covering a tutorial on Node.js. You can refer to the links below for a few node.js tutorials.

- ThenewBoston Node.js Youtube tutorials [ https://tinyurl.com/zgqjbu5]
Big fan of this channel. He’s fast, to the point. But although his videos are almost 4 years back, they must be still relevant.

- Node.js Docs [https://nodejs.org/en/docs/guides/getting-started-guide/]

To get started, let’s assume we have a working Node server running locally. And before we dive in, using git for version control in your project is mandatory!

We can divide this into two major steps

  1. Setup your Domain name and Web hosting
  2. Linking your Node project with the above two

Domain Name + Web Hosting setup

This should be easy. Go to any of the sites where you can purchase your domain, like GoDaddy or Namecheap, or any of the hundred other. And simply buy your domain name (hoping it’s available). I bought my Abhijit.dev on GoDaddy.

Choosing the web hosting service was the tricky part. At first, I was not too familiar with shared hosting vs VPS hosting. And after understanding the difference, especially the price gap, I decided to go with the shared web hosting, because I’m not building a social media website with some 10k + traffic. But, not all services support hosting node.js servers on their shared hosting plan. Finally, I found A2 hosting, which then supports what we intend to do, prices were fair and have good documentation + the cPanel has a software “Setup Node.js App” which we’ll later use to host our node app.

On A2 Hosting, I went ahead with the Swift Web hosting plan (mostly because of the 2X resources od CPU and memory).

You can buy your hosting on, http://www.a2hosting.com/refer/247786

Once that’s done, and we now have our domain name and web hosting is taken care of, let’s link these two.

Within your A2 hosting, go to the My Products & Services page, and click on Manage. This page should now list all the details of your hosting, along with 2 or 4 Nameservers. Make a note of the first two nameservers.

Now, switch to the site where you bought your domain name. Go into your products page, select your domain and find the domains Nameservers (On GoDaddy, search for DNS Management in your account). Here, you’ll need to paste the two Nameservers which you previously copied. And Voila! your hosting and your domain are now linked.

[Insert gif Voila! but not so far mister)

Linking your Web hosting with your Node.js app

Go to your domain’s control panel. In most cases, it should be [domain URL]/cpanel. And this should be login protected. While buying your web hosting and during the setup, you would have also set up the credentials for the control panel. But if you have idea, ping the support for the username and you could reset your password.

Clone your Node project using Git into your hosting.

  1. Search and click on “Git Version Control” in your cPanel
  2. Click on Create
  3. Make sure to have the “Clone a Repository” toggle on
  4. Enter your git repository’s clone URL and enter a suitable Repository Name. It could be the same your git repo name.
  5. Make a note of the “Repository Path”. You can change this too, but just make sure to copy this.
  6. Click Enter. This will clone your repository on to your hosting under that given Repository path.

Here’s a sample of Repository creation

Cloning your git repository

Node app setup on hosting

Once you are inside the control panel,

  1. Search for “Setup Node.js App” and select that.
  2. Click on “Create Application”
  3. “Application Root” should be your Repository Path from the previous step
  4. “Application URL” should already hold your domain name.
  5. “Application startup file” must basically point to the js file which creates the server.
    - If you have server creation in app.js, just enter app.js
    - If you created your node server using the Express framework, then it should “./bin/www”
  6. Click on “Create”. This will start the node app and should reload the page, with a “Detected configuration file” section. This should detect your package.json (packaje.json should be in the root folder of your repo)
  7. Click on “Run NPM install”. This is basically installing all your npm packages listed in your package.json
  8. Click on “Restart” and Voila! your website should be up and running.

You can also add Environment variables if your node app requires them. It’s usually a good practice to add your Database login credentials, Database host URL, and define other runtime flags.

Here’s a sample of Node app creation,

Node.js Web Application Creation

References

--

--

Abhijit Srikanth

Entering the medium world and finally begin writing articles on the technical front. With love towards the Virtual and Augmented world and all of WWW