How to deploy a NodeJS app on Heroku
Tags: node, heroku
Published: 2020-10-21 17:38:02

This post will be about the preparations that need to be carried out on a NodeJS root file(conventionally named index.js || server.js), and the Heroku side to successfully deploy a project written in NodeJS. 

I'll start with the NodeJS configurations that require to be written in the root file after initializing a project ('npm init' from the command line). 
I'll use the express library that runs in the NodeJS runtime, the library has helpers that make dealing with HTTP traffic easier.
  
NodeJS preparation
I initiate  a root file (index.js) and write the bare minimum code that gets the express server running. The code written includes the route handler 'app.get' that sends out the response on the screen "Hello": "World", and last line 'app.listen(PORT)' instructs express to tell NodeJS it wants to listen to oncoming traffic on port 5000.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7rp4xxyvnza2cezkl051.png)

Heroku Checklist
When I deploy my app to Heroku, Heroku will expect me to listen to oncoming http traffic on a particular port. I previously instructed NodeJS to listen on port 5000, but whenever I deploy my application to Heroku, Heroku will tell me which port I need to listen to. Setting up the dynamic port binding is a matter of configuring the 'app.listen(PORT)' code line in my index.js file. 
Heroku injects environment variables whenever it runs my application, environment variables are variables that are set on the runtime that NodeJS is running on. I can't figure out the port that my application will run on until the application has been deployed. The configuration in the code below shows how it is done.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9m574dfk0ogcmbkbiazt.png)

This set up handles the case where the application is running on a development environment at port 5000, and when the application has been deployed on Heroku and assigned a port.

The next step is to configure the package.json file with the versions of NodeJS and npm (node package manager) that my application is running on. I will write the engines object, where I'll specify the versions of NodeJS and npm which i can retrieve by running the 'node --version' and 'npm --version' commands from the command line .

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b1wcryxzjoqhbu7ek1dn.png)

Whenever Heroku boots up my application, it will read my package.json file, and check if I have declared any versions for use. Heroku will use the versions when it deploys my application.

The next step is to instruct Heroku which command to run in order start our server running. I'll configure the start script in the package.json file in the scripts object.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wwwtdqpb0r70qjuvptin.png)

I'll finally create a .gitignore file, this assures that we do not commit to version control (Git) any dependencies that we installed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p3orxei3xwe63jv5p58r.png)

Heroku Preparation
The first step is to sign up for a Heroku account on heroku.com. The process is straight forward and easy to fill out. Once you create an account log into Heroku.

The next step is to commit my code to Git (version control), Heroku uses a Git based deployment workflow. I will commit my code to Git as shown below

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ytvuefammrt0i0a25myt.png)

I will now install Heroku CLI, this will generate a new Heroku project. There are multiple ways to install a Heroku CLI.I installed the CLI using npm command; 'npm install -g heroku'. The command 'heroku --version' on the command line will show the version of Heroku that you just been installed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v8iq6ywl3i1yddyebmsk.png)

I will next login into an instance of heroku from the command line, I'll write the command 'heroku login' where I'll be prompted to enter my credentials, and then I'll create a Heroku application from the command line using the command 'heroku create'.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nl6ofw3yqi0fc64bso09.png)

The output contains two separate links. The first link is the address users navigate to when they visit my application in the browser. The name of the application is randomly generated by Heroku.
The second link is my deployment target. it is a Git repository where we can push our local server to.
I will copy the second link and in the command line I'll write 'git remote add heroku' and paste my link.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5l4g6596csjh709ofio6.png)

I will push the application shown below using the command 'git push heroku master'.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zaau4e5jhx1ld3bz9gkh.png)

The push command instructs that all the code committed to my master branch in the local repository is pushed to the Heroku repository.
Heroku will build my application, It will detect that I'm using NodeJS and install the versions of NodeJS I specified in my package.json file. 
It will install all my dependencies listed inside the package.json file, and the process completes when it reads that the build has succeeded.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a01poya3ex13576j7b03.png)

I will now test the application in the browser by writing the command 'heroku open'.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/35d6jqlxbui09sz0zrez.png)

This command will open my application in the randomly generated address provided by Heroku on my browser.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zbpgzov5lu7zsde5yuep.png)

My application is deployed.

However, if you get an error message the command 'heroku logs' written in the command line will generate the output from the deployment process. The logs will give an insight of what went wrong during the deployment.

To redeploy my application, I will use the command 'git push heroku master' after i have committed my changes to Git.