Monday, May 26, 2014

Cloudnant demo work on BlueMix

Last blog I discussed with Cloudant,but how do we use BlueMix application to visit Cloudant ? On the official site,it doesn't provide tutorial for that.Today I will show a node.js demo for Cloudant on BlueMix.Since node.js is one of my favorite language,also it provide  a ton of tools that make developing on CouchDB / Cloudant with Node.js effortless.You can read this blog to learn more about it.Finally I decide to choose express,Nano as my application library.I have put my code(you can use git to check out this url) at the jazz hub if you want to try it.

Installing the Required Modules

Express framework is required to run this app, to install the express framework, open the command prompt/shell and type the below command. The “–g” option simply means the module will be installed globally so that for each project we don’t need to install express separately.

    npm install –g express
    npm install nano (To install nano module)

Follow the Below Steps to Setup Our Project

After installing nodejs, execute the following commands in order-

    npm install express –g
    npm install jade –g
    express couchNano
    cd couchNano
    npm install nano
    npm link express
    npm link jade
    node app.js //to run the app

And go to browser (any browser) and type below in address bar http://localhost:3000. And press enter if you get the “welcome to express” page you have success created the basic structure required for this app.

Change or create  the createdb.js,index.jade,layout.jade,app.js.You can see the detail code at
https://hub.jazz.net/project/blackwhites/cloudant/overview.

In the app.js there is a piece of code you might need to config on your side .

var nano = require('nano')('https://username:password@yourdomain.cloudant.com:443');
/*Specify the database to use, so that we don't have to specify everytime*/
var db     = nano.use('yourdb')


You need to enter the correct username,password and your cloudant host address.Also you need to make sure you have create your db before in your hosted cloudant doamin.

After finish your code,you need to create a package.json file to add your node.js  dependency.

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.4",
    "jade": "*",
"nano": "*.*"
  }
}

Finish all the setting,you can open node.js command in your project folder  to use npm install command to install node.js module.After that use node app.js to run your sample.

Open the browser and use localhost:3000 to visit,Here I add Contacts as the sample


You can see the successful info after you submit the contacts info.

You can check the data in the your online Cloudant db.

After pass the local testing,you need to write manifest.yml to deploy on the BlueMix.You can see my sample file.
---
applications:
- name: cludanttest
  memory: 256M
  instances: 1
  host: cludanttest
  domain:ng.bluemix.net
  path: .
  command: node app.js

You can use cf push command to push your application to the BlueMix.

2 comments:

  1. Hi, its nice tutorial.
    But how can I update/ edit the json?

    ReplyDelete
    Replies
    1. bytheway, where I can get createdb.js,index.jade,layout.jade,app.js code?
      the link you gived is not valid anymore --> https://hub.jazz.net/project/blackwhites/cloudant/overview

      Delete