Deploy NodeJS with Mongo Db Project to Heroku. Easy setup


Even mLab MongoDB available in a free plan, you can’t add it to your NodeJS app from the Heroku dashboard without adding a credit card. In this post I included the easy steps to deploy NodeJS project with MongoDB. Also, I included the fix for other issues which may rice while running your app on Heroku.

Things to do before deployment process

1. Register a free account at mLab and create your database, then add user to it

2. In index.js (main js file in package.json) change your local mongodb connection string with the one you found in mLab database detail page… replace with your database user and with your database user password
eg. mongodb://sabeer:mypassword@xxxxxxxx.mlab.com:xxxxxx/mydatabase

3. Install Heroku-CLI … get installation details here

4. Go to .. dashboard.heroku.com/apps … Create app … you need this app name while deploying… so remember it

5. Replace your current listen port code with below one … else you may get Error R10 (Boot timeout)
.listen(process.env.PORT || 5000)

6. Create a file named Procfile without any extension then add the below code into it
web: node index.js

Replace index.js with your main js file name.. In case its a different one…

7. If you are using NodeJS default bcrypt … change it to bcrypt-nodejs else you may get some bcrypt errors on heroku server

Now You are ready to deploy your app to heroku… lets start

Deploy NodeJS app to Heroku server

1. Open terminal …

2. Enter
$ heroku login

Then it will ask you to enter heroku credentials… input the required credentials… if login success

3. Enter
$ cd my-project/
$ git init
$ heroku git:remote -a app-name

Replace app-name in 3rd command with your app name…

4. Enter
$ heroku config:set MONGOLAB_URI=db_connection_uri

Replace db_connection_uri with the connection URI… copied from mLab

5. Enter
$ git add .
Don’t forget to put the dot
$ git commit -am "your commit message"
$ git push heroku master

That’s it…

If you found any error… check logs by entering the command below
$ heroku logs --tail

 

sab99r

 

Leave a Reply

Your email address will not be published. Required fields are marked *

10 − 5 =