Thursday, June 19, 2014

Node.js With Express Framework Basic App on IBM Bluemix - Deploying Cloud Foundry

As i got request from +Yogesh Asthana brother by Google+ on Node.js app basics and other frameworks to build Highly scalable application that can be real time usage.Node approaches different manner in responding requests and while other scripting language make threads and use the RAM and core with full fledge usage and blocking approach.But Nodejs take care since it is written as Asynchronous functions with non blocking I/O behavior ,where when u Do some task in Nodejs like Database operations or file upload ,which is cost effective, network based and time consuming operations are made asynchronous where it will return the results later.here the next line code executes without any blocking.Thats why it is faster and may be due to other reasons to.This is built based on Google Chrome Run Time Javascript V8 compiler,by Google Engineers.
Node.js With Express Framework Basic App on IBM Bluemix - Deploying Cloud Foundry
nodejs with express i-visionblog

Demo | Download | Contact 

Scope : 

By this Post You could successfully Run a simple app Nodejs with Express in Blumix Cloud,IBM.
I have explained to build a simple app in my Youtube Video Channel.Any way i will Provide Explanation also by words below.

Downloads :

Visit Nodejs website and based on the operating system install the Nodejs in Your local machine for Locally testing your app before deploying cloud.

As my Last post gave You idea of IBM Bluemix,Just Create account in Bluemix and install Cloud Foundry Tool. - refer Here to install

Let Us Code : 

Basic Nodejs App contains Package.json file through which you can download all the dependencies and maintain your app efficiently even if you migrate from System.simply by using npm command you can fetch and install the dependency modules.You can simply initialize your package.json By command line

npm init  // will ask you one by one and you can provide details corresponding.

here i am explaining a simple app named sample and with description and others too.
package.json 

{
"name":"sample",
"description":"second app with node js",
"version":"0.0.1",
"private":true,
"dependencies":
{
"express":"3.x"
}
}

For Further Create a directory and do all the above operations.
now create app.js file to regulate and control our app.here app.js name is optional you can name it as per your needs.[main.js is mostly recommended]

app.js : 



var http = require("http");
var express= require("express");
var app= express(); // here we are defining our app and initializing with express 

//i am going to make basic routes for app

app.get("/",function(req,res){
res.send("<h1>this page is from Node js Express APP - Shivasurya - </h1>");
});

app.get("/welcome",function(req,res)
{
res.send("This welcome page ");
});


app.get("/about",function(req,res)
{
res.send("This is from About page ");
});

app.get("/example",function(req,res)
{
res.sendfile('example.html');
//the above fun send html file to browser so that browsers can intrepret
});


//similarly you could add routes and send the text as html or even html files to the browser using res.sendfile();


http.createServer(app).listen(3000); //here 3000 is port number for app



//thus we have created a simple web app using Express framework

In the Above Code we created Express object of response and request and we accessed send() and sendfile() functions.there are still more functions can be invoked,by next post i will brief it about the Routing techniques and best practices.Open Your browser point to localhost:3000 and check the routes we defined already in app.js

Time To Deploy On Cloud :


Check that You have Installed by Using the Command in Your Command prompt.

C:\>cf -v  

must output the version number of your CF Tool installed currently.Move to Your Current Directory where your project lies.We need Small Changes in Our code since it is Production Environment however your going to test in on Cloud as Sand Box mode.

Code Changes For deploying at Cloud :

thus we had Simply added VCAP service Port Number and Host to work perfectly on cloud environment.

var port = (process.env.VCAP_APP_PORT || 8192);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require("http");
var express= require("express");
var app= express(); 


app.get("/",function(req,res){
res.send("<h1>this page is from Node js Express APP - Shivasurya - </h1>");
});

app.get("/welcome",function(req,res)
{
res.send("This welcome page ");
});


app.get("/about",function(req,res)
{
res.send("This is from About page ");
});

app.get("/example",function(req,res)
{
res.sendfile('example.html');

});


http.createServer(app).listen(port,host); 



now create a Manifest.yml file to instruct Bluemix to allocate Nodejs Applications,space Instance and others.i have explained in my Last Post on Creation and Usage of Manifest.yml.

Manifest.yml


---
applications:
- name: sampleapp1995
  memory: 256M
  instances: 1 
  host: sampleapp1995shiva
  command:node app.js

Just Push the App using the command below in Command prompt

$ cf push
Progress :


finally after deploying
during installation depedencies automatically screenshot


note: Work with caution because cloud may costs you money for over usages and scalling instance.And you can use HTTPS connections too.

I have deployed my application on Bluemix.just have a try since it is free till june month 2014.just share your experience with comments and post errors.Contact with me in [email protected].Share is care.

1 comments:

Srivathsan said...

Got this error... So I had to leave a space between : and node in the last line of manifest.yml
Error reading manifest file:
yaml: [while scanning a simple key] could not find expected ':' at line 8, column 1



And another doubt.. What does instances mean in the manifest file ?

Post a Comment

feel free to post your comments! Don't Spam here!