Steps to Deploy Node.js Application to Heroku using MongoDB and MLab
Heroku Setup
-
First create a Heroku Account
-
Read Heroku's documentation thoroughly. Check it out here
-
Setup Heroku CLI as instructed in the above link
-
Create and deploy the app as instructed here
-
Push your project folder to Heroku server
mlab
Before checking out your app, follow the instructions below to set up-
Setup an mlab account
-
Go to
MongoDB Deployments
andCreate a new deployment
-
Choose a required plan and select that database
-
Create a
collection
and give it a name of your choice. Click onAccount
and setusername
andpassword
to give access to database (Not your login credentials for mlab). -
In
server/index/app
configure port:
port: const PORT = process.env.PORT || 5000;
- In
db.js
,MONGODB_URI
set up:
const CONNECTION_URI = process.env.MONGODB_URI || 'mongodb://localhost:<your_id>/<name_of_your_db_folder>'
(MONGODB_URI
is available only in production environment, to set it up, follow the next step)
- Then in Heroku CLI, setup MongoDB URI by running the command:
(
ds ID
andport
might change, please use the URI provided to you)
heroku config:set MONGOLAB_URI='mongodb://<your_username>:<your_password>@ds223653.mlab.com:23653(your mlab collection link given by mlab)/collection_name'
- If
MONGODB_URI
is not supported, then useMONGOLAB_URI