paint-brush
Heroku’s handshake with mlabby@_Obbap
1,188 reads
1,188 reads

Heroku’s handshake with mlab

by Paschal ObbaFebruary 14th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A Platform-as-a-Service(PaaS) saves us the time required to set up virtual servers, makes scalability easier and depletes the cost implication of setting up the servers.
featured image - Heroku’s handshake with mlab
Paschal Obba HackerNoon profile picture

A Platform-as-a-Service(PaaS) saves us the time required to set up virtual servers, makes scalability easier and depletes the cost implication of setting up the servers.

Initially, setting up a server in the cloud required connecting and configuring all types of components but the introduction of Serverless,Docker,Heroku and the likes have allowed us focus on only the application.

We will be merging our MongoDB cloud service(mlab) with our PaaS(Heroku). This is important because while using PaaS, it allows us concentrate on developing and managing our applications without being perturbed by the thought of maintaining the infrastructure.

To go through this process successfully, we need to have:

Now we have everything up and running, we can create a new app on our Heroku account. For the sake of this project, the name of the app is ‘mediumapp11’.

Then we create a database on mlab, with the name ‘mediumapp11’. When we have our database up and running, we can add a database user and then we will see our MongoDB URI above. This will help us connect to our database from our localhost.

We will have something of this nature:

mongodb://<dbuser>:<dbpassword>@ds0*1*7*.mlab.com:41678/mediumapp11

Where the ‘dbuser’ tag is the username of the user you created and the ‘dbpassword’ is the password of the user.

Now on our local host, we can create a ‘mediumapp11’ folder and then do this:

heroku addons:add mongolab --app mediumapp11

Where ‘mediumapp11’ is located, the name of your app will go there. When this is done, we should find mlab as an addon in our Heroku project.

Now, we can finally create a ‘mediumapp11.js’ file. We will need the mongoose dependency to define our schema and http.

const mongoose = require('mongoose');

const http = require('http');

const Schema = mongoose.Schema;

Then now, we can define our schema ,create a document which is the instance of our model and then specify the URI we had gotten from mlab.

let Team = mongoose.model('Team', TeamSchema);

let db = mongoose.connection;

let dbUrl = 'mongodb://mediumapp11:mediumapp11@ds041678.mlab.com:41678/mediumapp11';

Then, we can listen for errors and if there is none we connect to our URI and save a particular document.

db.on('error', function () {console.log('error');});

mongoose.connect(dbUrl, function (err) {

if (err) { return console.log('there was a problem' + err); }

console.log('connected!');

var team = new Team({ name: 'Jehovah' });

team.save(function (error, data) {

if (error) {console.log(error);}

db.close();

process.exit();

});

});

Now, we should ensure we replaced the ‘<dbuser>’ with ‘mediumapp11’ and the ‘<dbpassword>’ with ‘mediumapp11’ and then we can run the app

node mediumapp11.js

We can now refresh our mlab, then go to documents and then we will find our our document saved.

If you learnt anything, let me know by 👏👏👏.