Saturday 19 January 2013

JavaScript Introduction

       JavaScript is a light weight ,interpreted programming language. It is the most popular scripting language which is used by many popular browsers like Mozilla, Safari,Opera,Chrome etc.
      JavaScript is embedded directly into Html Pages.

Features of JavaScript:

Embed into HTML:
 JavaScript can be easily embedded into HTML pages.

Events :
It will react to events like clicking button,finish loading page etc.

Read/Write HTML elements:
It is used to read/write HTML element.

Validation:
It is used to validate form data beform submitting to the server.


Thursday 17 January 2013

what is node

-->
        

                Node is a pltatform built on V8 which is already used by Google crome.It was created by Ryan Dahl.In order to execute javascript,
             Node is a platform for writing serverside javascript for writing scalable internet applications where as Javascript is for writing client side script.It was created by Ryan Dahl.
Every function in node is Asynchronous,nonblocking i/o.
Node applications uses asynchronous event driven model.

Node consists of lots of modules and no need to write from scratch. Node is a combination of tho things, run time environment and a library.

  

 

 



Thursday 10 January 2013

Non Blocking I/O in Node.js

-->




Difference between Blocking and non blocking

Blocking:
In blocking, we can read file one after the other in a time interval.



Non Blocking:
In non blocking, we can read files at same time.

Call Back in node.js

-->
We can add call back in node.js in two ways
1.Write a call back inside a method
Ex:
fs.readFile('/abc/def',function(err,data) {
console.log(data);
});
2. Create a variable for call back and add call back to the function.
var callback=function(err,data){
console.log(data);
}
fs.readFile('/abc/def',callback);

Tuesday 8 January 2013

Filesystem module (fs) in node.js

File system module is to access files on the disk.

To use the module, use   "require(fs)  "  and all the methods have synchronous and asynchronous forms.

Asyn form always takes completion call back as a last argument and 1st argument for exception. If the operation completed successfully, it returns null or undefined.

see this example

var fs=require('fs');
 fs.exists(nodemongo.txt,function(exists){ 
if(exists)
{
fs.stat(nodemongo.txt,function(error,stats){
fs.open(nodemongo.txt,function(error,ab)
{
var bf=new Buffer(stats.size);
fs.read(ab,bf,0,bf.length,null,function(error,bytesRead,buffer){
var txt=bf.toString("utf8",0,bf.length);
fs.close(ab);
    });
 });
)};
 }); 

Event mechanism in node.js

Node.js is an Event driven like Dom. When user interacts with particular interface, then there is an event.

Node.js created EventEmitter class to provide basic event functionality.

require('events').EventEmitter  to access EventEmitter.
This class contains different events


1.emitter.addListener(event,listener)  or  emitter.on(event, listener)

all the EventEmitters emit event newListener when new listener ads.


session.on(change, function())

session changed.

ex:

server.on(event,function(p,q,r){

});



2. emitter.once(event,listener)

it adds one time listener for event and it invoked only next time event fired.

 3.emitter.removeListener(event,listener)
it removes a listener.

 4.emitter.removeAllListener([event])

it removes all listeners.

5.emitter.setMaxListeners

defaultly it will print warning if more than 10 listeners added to particular event. Set zero for unlimited listeners.

6.emitter.listeners(event)

It returns an array of listeners for an event.

7.emitter.emit(event,arg1[],arg2[],.....)

it executes each listeners in an order.