Skip to Content

Hi there,

I just wanted to blog a little about using node.js on CloudFoundry. Wether node.js is a possible solution for your business or not and what are the advantages and disadvantages I’ll let you decide, there are plenty of other frameworks than loopback, each with their own strength and weaknesses I just choose it because I wanted to look into it. I am no “This-Framework-rules-them-all” kind of guy, there is always an analysis upfront where eventually a framework might evolve to meet your business requirements. This is just a technical blog about crazy nice stuff 😉

So let’s get started.

  • I assume you already have an HCP CloudFoundry Account and the cli installed, if not reffer to Rui Nogueira’s blog
  • You have node.js installed, if not click here
  • Next thing we are going to install loopback.  Basically you just can follow the “Getting Started” section of loopback’s website, I did nothing else, I just want show you the CF specifics here.

Assuming you followed the instructions to the end(basically you just press enter all the time) you should have something like the following:

1.PNG

Now let’s create a manifest.yml (in the root folder of your app) so we can push the app to cloudfoundry.


---
applications:
- name: hello-moon
  command: node .
  buildpack: https://github.com/cloudfoundry/nodejs-buildpack
  memory: 128M
  services:
  - mongo-service


before we push, we want to ignore the node_modules folder. So create a file .cfignore and put the following contents in there


node_modules/


there is normally more stuff in here, as well as in the .gitignore file but leave it like this, what happens is, you reduce the upload size from around 36MB to 8KB, nice and fast 😉

Now issue the following commands:


cf api https://api.cf.us10.hana.ondemand.com 
cf login
cf push


the result should look like the this:

3.PNG

You can go to the cockpit and call your app

4.PNG

The result should look like:

5.PNG

but wait, what about persistence? We didn’t specify anything, except that we need the MongoDB service? So let’s test it:

In the API Explorer(picture above) click on the POST /people and you will get this screen, put in some data..”Try it out!”…

6.PNG

Result:

7.PNG

Seriously? Why is it working? Let’s check if there is really some data…

8.PNG

it is… to solve the mystery currently we use an inMemory Database, which you can check in the datasource.json in the server folder of your app.

So let’s tell loopback to use our MongoDB instead:

First you have to note down a few things. Go to your cockpit and click on “Service Bindings” and press the “Show Sensitive Data” Button.

10.PNG


Pease note in a real project I wouldn’t do it like this, because in all your commits your database user and passwords would be visible for everyone. Ideally you would access MongoDB environment variables as proposed by the 12factor Guidelines but back to topic:

Type


slc loopback:datasource

in your app root folder choose  MongoDB connector and enter all required information (host, uri, user etc.)

9.PNG

Now edit /server/model-config.json

and change the line

11.PNG

to your datasource in my case it was MongoDB

12.PNG

push your app (cf push)

And test it 🙂 unfortunately I dind’t find a way to read the contents of a MongoDB in cloud foundry but maybe someone from the HCP Team can help us out here.

But to verify the result i wrote a little script to read out MongoDB:


var MongoClient = require('mongodb').MongoClient
var http = require('http')
var url = 'mongodb://allYourSecretStuff'
function handleRequest(request, response) {
    var result;
    MongoClient.connect(url, function(err, db) {
        if(err)throw err
        console.log("Connected correctly to server.")
        var a = db.collection('person')
        var b = a.find().toArray(function(err, docs){
        db.close()
        response.end("Found " + JSON.stringify(docs) + "  in MongoDB")
        })
       
    })
}
var server = http.createServer(handleRequest)
server.listen(process.env.PORT || 3000, function() {
    console.log("Server up and running ")
})

Please note you have to replace


var url = 'mongodb://allYourSecretStuff'

with your own MongoDB Url from the sensitive Data Step.. Create a packacke.json


{
  "name": "mongo-check-people",
  "version": "0.0.1",
  "author": "Demo",
  "engines": {
    "node": "4.4.4",
    "npm": "*"
  },
   "dependencies": {
        "express": "3.4.8",
        "mongodb": "~2.0"
  }
}

and a manifest.yml(take the one you created already and copy it) and deploy the app. in the end you should see your data from MongoDB

14.PNG

That’s it for today, i hope you enjoyed the MOON stack. We didn’t go very deep into what is possible with loopback and node, we have no UI yet, Authentication is missing and so on.

Let me know what you think, if I should continue the journey, enlighten me on points i missed and generally have fun 🙂

Regards Mathias

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply