Deploy an Express API (Node JS) on Heroku - BOOKC -->

Deploy an Express API (Node JS) on Heroku

Saturday, February 9, 2019

Hey everyone! Today I brought you a different type of article. This is about deployment! A practical guide.. Although we develop applications on local machines, we should get and experience on deployment of an app on a remote server or host also. It will be a nice experience because you can give a public URL to anyone to access and see your work!

This is my live API on Heroku. Check it by adding api/users at the end of URL.

I'm going to use a well know cloud application platform called Heroku in my deployment process. If you don't know about Heroku, visit below link to get an idea.

To start our task, you should have some prerequisites.

  1. Express API should be implemented
  2. Heroku CLI should be installed
  3. GIT should be installed
  4. Remote MongoDB should be integrated into application

Step 1 - Clone API

So, you can get an express API which I have already implemented from this GitHub link. Clone it and keep ready for modifications.

Step 2 - Install Heroku CLI

Install Heroku CLI from here according to your OS. CLI means Command Line Tool that is used to perform heroku commands via terminal/cmd.

Step 3 - Connect MLab as remote DB

My GitHub application has been already connected to a remote MongoDB service instead of local database. You may have already heard MLab..That's the DB as a service I'm using here.

Register in this site and create a database to be used in any MongoDB application. Then connect the DB as they have defined in their site. Basic format or DB URL is;


NOTE: Don't use characters like @, _, - when you define password for users.

Create a collection called users in your database. Then it will be used throughout the application.

Step 4 - Deploy on Heroku

Now we are ready to start deployment! Go into your project and check whether this configuration is there.
there should be "scripts" section with "start" key. Give your main JS file runs.
  "scripts": {
    "start": "node app.js"

This will take care of starting your application after deployment. This must be correct according to your app! Otherwise Heroku fails.

Now open your terminal/cmd and go into project folder. Then follow the below commands.

git init echo node_modules > .gitignore git add . git commit -m "Add app to Heroku"

NOTE: Second command push the node_modules folder into .gitignore file. Then It will not be uploaded via GIT into Heroku.

Now it's time to create an application on Heroku with a preferred name. It will create an application with a URL like If it says that this name is already taken, give another!

heroku create express-api

Now all are set to push the application to Heroku. Perform this command!

git push heroku master

It will take some time.. Heroku will check on start scripts we configured earlier. Then it builds node_modules in the remote host by running npm install automatically. If all the steps have been completed successfully, now you app is ready to be used in public.

Go to URL and see the response is coming! In my express API I have set a route for home.. Then it will return a string "Hello from server".

You can check whether it's coming! If it displays an error page, find the errors via Heroku logs.. 

Hit this command to see logs on terminal. =>  heroku logs

Ok guys! If you have followed me, now your API should be up and live on Heroku! Isn't it great? You can access it in any application now! No need to run a local server to access in applications.. 

If you want to modify code and update Heroku?

Just go into the local project folder and do the changes. Then save all changes. Run these GIT commands and finally push it again to Heroku.

git add . git commit -m "Add changes"
git push heroku master

So, now we have come to the end of our application! This is not so hard thing to do guys! Try this!!!
Heroku has implemented which can be used by any developer easily. No hard things! Try this out and give me suggestions..

Good Luck!