This tutorial is meant to make it easy for anyone who is hosting a nodejs application that uses the mongoDB database. If you have already checked out my other heroku tutorial using postgresql, then this tutorial will just be a piece of cake.
In any case, I will assume that you are here for the first time, so I will run you through a beginner process. We will be hosting the authentication app we completed in the last tutorial.
- Clone the project here.
- Delete the
.gitfile that may have come with it. Another
.gitfile will be created for you in the process of you pushing to GitHub.
So without further ado, let's get our hands dirty.
Uploading or pushing to GitHub is as easy as taking your favorite meal. Please checkout any of the following links to get directed on just how to push your project from you local machine to GitHub
I am going to make this process so easy that you will just smile throughout.
- Let's begin by creating an account on Heroku
If you have created an account, you may have been prompted to create an app (i.e. a folder where your app will be housed). Create it. Mine is named
- Go to your app's dashboard
- Select the
- Search and select a repo
- Click on
- Select the branch you want to deploy (In my own case, it is the
Enable automatic deployment by clicking
Enable automatic deploymentbutton as in the image above.
Click on the
Deploybutton in the manual deploy
We will not have to do all these for subsequent deployment
- Now you have a button telling you to "view site" after build is completed. Click it. (This will open your app in a new tab)
OHHH NOOOO!!!! A BUG? APPLICATION ERROR?
Well, it just a small issue. Something you should never forget to do while making deployments. Most hosting service will require it
Get back to the root directory of your project
- Create a file and name it
Procfile(It has no extension)
- In the file, enter the following
web: node index.js
This directs Heroku to the server file (
index.js) which is the entry point of the application. If your server is in a different file, please modify as required
- Save the file
- Push the new changes to GitHub
- Wait 2 to 5 minutes for Heroku to automatically detect changes in your github repo and effect the changes on the app.
- You can now refresh that error page and see your hard work paying off
You must have noticed that other routes are not functional. Yes, it is because we have not included our database.
Remember that the URL to our database is in our
.env file. But our
.env file is not included in our project on github after we pushed it. So we have to directly add the mongoDB URL into our heroku app.
Let's do that...
- Navigate to the settings of your app
- Scroll down to the
- Add the key and value of your database
That is all!!! Your App should be working fine now...
The easiest way to test if it is working is to try the login endpoint
We started out by cloning a the project from our last tutorial. Next, I pointed you to how to push a project to github. We then created a heroku app where we hosted out project from github with ease. Finally, we added our database URL to the app and our app works perfectly like it worked locally.
I hope you have enjoined every bit of this tutorial as I have enjoyed while creating it.
See you in my next tutorial.