DEPLOYING YOUR NODE/EXPRESS APP ON HEROKU

Amadi Austin Chukwuemeka
Studevs
Published in
3 min readSep 1, 2019

Deploying node applications in the server can be a little difficult especially to developers who are new to Node. Here am going to walk Your through on how you can deploy any node/express app on the Heroku server.

1. Deploying Static Node/Express Apps: By static apps I mean any express application that don’t involve external functionalities like implementation of a Database or any Api integration. Hosting this kind of application in heroku is quite simple and don’t really involve much.

Before you follow the following steps I believe you project is running well on your local host.

Step 1: Install Heroku CLI.

You can download from this link

After that, verify it by running the following command (if you are on windows)

heroku -v

Step 2: Create Your Procfile

Inside the root of your folder, create a Procfile. It is the Heroku file in which you will define the dynos setting like after uploading the app, how heroku will start the node js web server.

Your Procfile should look like this (specify your project entry file) like app.js, index.js or anything you named it. Like mine here is sever.js

// Procfile

web: node server.js

Step 3: Heroku Logging from Terminal.

Open your terminal inside the root of the project. Type the following command to log in to your Heroku command.

heroku login

You need to log in with your registered email and password.

Step 4: Deploying Your Node App to Heroku.

You must initialize your project to a git repo using the following command.

git init

Then type the following command.

heroku create <app-name>

This will create your app in the heroku server using the specified name.

Is now time to push your files to the Heroku platform.(Using the git commands)

git add .git commit -m “Deploying to heroku"

Push the changes to the server.

git push heroku master

After that, you can see that we have successfully deployed our node js application.

Step 5: View Your Heroku App on The Browser.

In your terminal, type the following command to open the application in live.

heroku open

This will open your app up in the browser and Congrats!! Your app has been deployed.

2. Deploying Dynamic Node/Express app: By dynamic apps I mean any express application that involve external functionalities like implementation of a Database or any Api integration. Many do get stuck at this stage. So let’s get started.

NOTE: FOLLOW THE STEPS IN STAGE ONE BUT STOP AT STEP4 (DON’T RUN THE HEROKU OPEN COMMAND YET BECAUSE YOU WILL GET ERRORS.

1. Create your .env File: This is a file that will contain your app secret key and password values. The will be stored in a key and value pair. You can read more on how to create your .env file here “Node.js Everywhere with Environment Variables!” by John Papa https://link.medium.com/asmz0OOPCZK

2. Set it up to heroku: After setting your .env files, you have to set it up on heroku as well.

Run the following commands on your terminal

heroku config:set <key=value>

Note: You must do this for all the key and value pairs in your created .env files.

Note: If you are using Atlas mongo DB database, go to your Atlas Dashboard under security,click on Network Access, then click on Add IP Address, on the dialog box that will appear you can choose allow any IP address to connect to your cluster.(This will save you some stress) or you can specify your own IP address.

3. Setting the port number: This involves allowing heroku to dynamically assign a port number to you application.

Add this line of code in your app entry file if is not there, especially if you used an Express Generator.

app.listen(process.env.PORT || 8000);

Step 4: View Your Heroku App on The Browser.

Save all your changes and run the following command.

git add .git commit -m "I added process.env.PORT "

Push the changes to the server.

git push heroku master

In your terminal, type the following command to open the application in live.

heroku open

This will open your app up in the browser and Congrats!!Your app has been deployed.

--

--

Amadi Austin Chukwuemeka
Studevs
Writer for

I am a Software Engineer who is passionate about solving complex problems by Building technological solutions out of business needs.