THAPA TECHNICAL

HOUSE OF WEB DEVELOPERS AND TECHNOLOGY.

How to Host Node Project in Heroku for Free in 2020


How to Host Node Project in Heroku for Free in 2020


What is Heroku?

Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps.


How to Host Node Project in Heroku for Free in 2020


Here is the official page of the Heroku website. Check Here

First, We need to do the registration and make sure to add the proper working email ID, because after the registration part Heroku will send you a confirmation message of registration with a link attached to it.
You have to click on that link in order to get verified. 








Once Registration is done, then we need to login to Heroku. In order to start creating our app.



 

Once login is done. Then we need to install the Heroku CLI in order to manage it through CMD or any default prompt terminal. 




 To install the CLI, Minimum requirements

The Heroku CLI requires Git, the popular version control system. If you don’t already have Git installed, complete the following before proceeding:


How to install CLI?


In this step, you’ll install the Heroku Command Line Interface (CLI). You use the CLI to manage and scale your applications, provision add-ons, view your application logs, and run your application locally.


Download and run the installer for your platform: Check Here






Step 1: Now, we will check whether is install properly or not

Command=> heroku -v

heroku/7.46.0 win32-x64 node 2-v12.16.



 





If you see, the out like this. Then hurray. It’s installed successfully

 

Step 2: Now we need to add the ssh public key to Heroku to securely send the code.

Before adding SSH Keys, make sure you have one in your local system.


# 1. check for SSH keys.

# check exists file id_rsa.pub or id_dsa.pub.

$ ls -al ~/.ssh


# 2. nothing the id_rsa.pub or id_dsa.pub file, type this command.

$ ssh-keygen -t rsa -C "your-email@email.com"


# listen to save the file, press "enter" and setting your password.


# add your new key to the ssh-agent.

$ ssh-agent -s

$ ssh-add ~/.ssh/id_rsa


# 3. add your ssh key to GitHub.

# for example...

$ clip < ~/.ssh/id_rsa.pub

# and go to your GitHub setting page, adding ssh key.


# 4. test everything out

$ ssh -T git@github.com


Once you are done with SSH then type 

ð  Heroku keys:add

 

This will show the id_rsa.pub file and ask, whether you want to add this ssh public

File or not.



We need to click on Yes.

 

Step 3: Need to create an App or give the name of the project to be add on Heroku

ð  Heroku create “AppName”

**remember we need to use only the small letters and dashes only.  

 


 

Step 4: We need to change the script name to make sure to run the code on Heroku as well as in the local system.



Step 5: We need to change the port number and It’s not something we need to write manually. It may change every time when we run Heroku but what we need to do is simply write one line of code in our main app file.

 

ð   cosnt port = process.env.PORT || 3000


ð       

    const express = require('express');

ð         const path = require('path');

ð         const hbs = require('hbs');

ð         const app = express();

ð         const port = process.env.PORT || 3000 ;


 

And last we need to add the port variable in our listening code.



 app.listen(port , () => {

    console.log(`listening to the port at ${port}`)

 })


 


Now, finally, it’s the time we are going to add our code to Heroku by simply writing one line of command. But before that one line of code, we need to make sure that our file is up to date with GitHub.

So, we will first check

ð  git status

ð  then if any modified files

ð  git add .

ð  git commit -m “comment you want to add”

     Now we are finally ready to write that one line of command.

     Before that, we need to check the remote in our app.

ð  Git remote

Heroku

Origin

 

And that is

Git push Heroku

Hurray, this is it.

 

2 comments:

  1. Great tutorial sir, but mera ek issue hai, jab mai terminal me ja k src k andar se node app.js ko run kar raha hu to sab kuch thik chak raha hai , but jab mai main project se node src/app.js likh raha hu to error de raha hai k "failed to lookup views"index" in views directory, Please thoda sa help kar dijiyea

    ReplyDelete