Deploying Node and MongoDb app in Heroku

Shouvik Bhuiyan
3 min readDec 4, 2019

--

Heroku is a popular choice for deploying node.js apps and deploying in heroku is fairly simple.

Considering that we have heroku cli installed globally and mongo instance running on local, the following steps will create a node and mongodb app, deployed in heroku.

We will use remote mongodb instance for accessing database from heroku and will use mlab atlas for the same. Since mlab doesnot support new registrations, we need to signup in mlab atlas and follow onscreen instructions to create a new cluster there.

At the end of cluster creation it should look like the following

Next under the Database access tab create a new user and give relevant permissions to the same. Also, make sure to whitelist your ip under Network access tab.

Once the remote mongo instance is up and running we come back to code and create a config folder in our root. Add mongo.json under config and add the following code.

{“development”: {“host”: “127.0.0.1:27017”,“dbName”: “user-db”},“production”: {“env-variable”: “mongodb+srv://<user>:<password>@cluster0-yanec.mongodb.net/test?retryWrites=true&w=majority”}}

here we are creating two configs one for development which has local mogodb url host and under production key we have the url for remote mongo instance.

Next in our connection.js file we do the following to create a connection between our app and mongoDb.

We are using mongoose here to connect to db.

const mongoose = require(‘mongoose’);const config = require(‘../../config/mongo’);const env = process.env.NODE_ENV || ‘development’;const localUrl = `mongodb://${config[env].host}/${config[env].dbName}`;const prodUrl = config[env][‘env-variable’];const connectionUrl = prodUrl ? prodUrl : localUrl;const connect = mongoose.connect(connectionUrl, {useNewUrlParser: true});module.exports = connect;

Notice that we are creating connection url based on the environment we are using. Unless NODE_ENV is set, we will always use the development url.

Once the code is done and ready to be deployed, go to application root directory and create a heroku instance by running

heroku create

Add a mongodb database in heroku by running

heroku addons:create mongolab

Note that we had used NODE_ENV to detect a production environment. Add the value by running the following.

heroku config:set NODE_ENV=production

We can see all the configs for by running

heroku config

Last but not the least add a procfile in project root directory to tell heroku your app stating point. In my case, all my server code is under server/ folder. So i have added the following inside procfile.

web: node server/index.js

This is how my folder structure looks

Now that all the settings are done we should be able to deploy in heroku. Check in all the codes in github. Make sure package.lock and yarn.lock files are not checked in and run the following command.

git push heroku master

This will create a heroku application with a random container name. we can see the application in browser by running

heroku open

Our application with node and mongo should be up in heroku by now. In the comment section, let me know if you are facing any trouble.

--

--

Shouvik Bhuiyan
Shouvik Bhuiyan

No responses yet