How to host your backend code on Heroku

Ayush Kumar
5 min readJul 8, 2021

I have seen and experience it myself once, as a new developer, most of us find it very troubling to understand how to deploy the backend code so as to use it in our projects. This article will summarize all the necessary points which will guide you to deploy your first backend code on Heroku.

So before going further the first question that arises is what is Heroku?

Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. This platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market.

Heroku is fully managed, giving developers the freedom to focus on their core product without the distraction of maintaining servers, hardware, or infrastructure. The Heroku experience provides services, tools, workflows, and polyglot support — all designed to enhance developer productivity.

So now that you know what is Heroku the second most important question is how do I use it to deploy my code? Let’s get to it.

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.

Here is the screenshot of the confirmation mail sent by Heroku

You have to click on the confirmation link and verify your account. Once the registration is complete you will have to log in to start deploying your app.

To log in just enter your registered email and password. 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, make sure to install it before proceeding.

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: click 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 get similar output then heroku cli is installed.

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@gmail.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 key type

heroku keys:add

This will ask if you want to add ssh key to Heroku and then automatically add ssh key onto Heroku.

Once the ssh key is added then

follow following steps to upload code on heroku and deploy it

  1. git status — To check the current status of the project whether git is initialized or not or to check the untracked changes in the project.
  2. git add . — This will track all the code in the project and add all code to git staging area.
  3. git commit -m “message” — The git commit command captures a snapshot of the project’s currently staged changes. Committed snapshots can be thought of as “safe” versions of a project — Git will never change them unless you explicitly ask it to.
  4. heroku login — This command will connect your terminal with heroku and create a secure tunnel for you to transfer the code.
  5. heroku create “appname” — This command will create a new app in the heroku platform but you must be logged in to create a new app.
  6. git push heroku master- This command will send all your committed code to the heroku and will start automatically deploy it.Here master is the branch you are currently working from,if you are on some other branch write the name of you current branch instead of master.

These are the required set of commands to deploy the code but before deploying the code make sure you do the following changes in your project.

Heroku will not accept any random port you send it to start the project so you will have to write

const port =5000 || process.env.PORT

Also add “start”:”path of the server.js file” in your package.json file in your project.This command will let heroku start the project when you deploy it.Please provide the path for the file which start your node file for me it was server.js ,but it might be different for you.

And lastly even after doing all this after deploying your code if you encounter any error then write

heroku logs — tail this will show you the whole deployment process and also the error you might have encountered.

Thank you for going through the article.

--

--