Wednesday, September 11, 2013

Different Type console output in [Node.Js]

console.log([data], [...])
console.info([data], [...])
console.error([data], [...])
console.warn([data], [...])
console.dir(obj)
console.time(label)
console.timeEnd(label)
console.trace(label)
console.assert(expression, [message])

Usage of console.log([data], [...]),console.info([data], [...]),console.error([data], [...]),console.warn([data], [...])

 Print the output in a newline .
This function can take multiple arguments like a printf() in C or System.out.println() in Java



Example Consloe.js:
 var data=[{"name":"Kotini","LastName":"Tirumula"},{"name":"Kotini","LastName":"Srinu"}];  
 console.dir(data);  

Out Put:

 D:\nodejs>node Console.js
 This is console log output
 This is output of,console log

Usage of console.dir(obj):

Example ConsloeDir.js:

 var data=[{"name":"Kotini","LastName":"Tirumula"},{"name":"Kotini","LastName":"Srinu"}];
console.dir(data);

Out Put:

D:\nodejs>node ConsoleDir.js
[ { name: 'Kotini', LastName: 'Tirumula' },
  { name: 'Kotini', LastName: 'Srinu' } ]
D:\nodejs>

Usage of console.time(label) and console.timeEnd(label)
console.time(label):
 Mark or start record a time.
console.timeEnd(label):
 Finish timer, record output. 

Example ConsloeTime.js:




 console.time('10-elements');  
  for (var i = 0; i< 10; i++)   
 {   
   console.log("Itr "+i);   
 }   
 console.timeEnd('10-elements');  

Out Put:
D:\nodejs>node ConsloeTime.js 
Itr 0 
Itr 1 
Itr 2 
Itr 3 
Itr 4 
Itr 5 
Itr 6 
Itr 7 
Itr 8 
Itr 9 
10-elements: 2ms 
D:\nodejs>

Usage of console.trace(label)
  Print a stack trace as a stderr at the current position.And break the execution.
Its kind of throwing exception. 

Example ConsloeTrace.js:


 console.log('1st Line');  
 console.log('2nd Line');  
 console.trace('trace Here');  
 console.log('4th st Line');  

Out Put:

D:\nodejs>node ConsloeTrace.js
1st Line
2nd Line
D:\nodejs\ConsloeTrace.js:3
console.tace('trace Here');        ^

TypeError: Object # has no method 'tace'

    at Object. (D:\nodejs\ConsloeTrace.js:3:9)

    at Module._compile (module.js:456:26)

    at Object.Module._extensions..js (module.js:474:10)

    at Module.load (module.js:356:32)

    at Function.Module._load (module.js:312:12)

    at Function.Module.runMain (module.js:497:10)

    at startup (node.js:119:16)

    at node.js:901:3



D:\nodejs>

How to write Hello World in [Node.js]

Hello world is a most important program for a developer.

Node.js Installation and check its working or notSystem Environment Variable

I will briefly tell you how to write "Hello In Node.Js" in 2 steps.

Step 1:
 Create a text file with extection js.
 like HelloWorld.js and I saved in D:\nodejs\HelloWorld.js





Step 2:
 Open Command prompt or terminal  ,got to the saved folder and type "node HelloWorld.js"
D:\nodejs\>HelloWorld.js

Out Put:

D:\nodejs>node HelloWorld.js
Hello World
D:\nodejs>





Node.js Installation and check its working or not

About Node.js:
Theoretically :  
  Node.js is a platform built on Chrome's JavaScript run time for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

As per my Experience:
Node.js is a platform built on Chrome's JavaScript runtime ,which we can use server side.Till now developer uses Javascript as client side .But Node.js will help to write server side programming using Javascript.

Download And Install Node.Js from below link:
http://nodejs.org/download/
Once you install how to check Node.js is working or not
Open Command prompt or Terminal and type "node" and check for ">" console.
Once its showing then cancel the Command prompt or  terminal .

e.g:


After installation if Node.js is not working then:

Set Environment Variable: Click Here To Check set Environment Variable
Variable Name: NODEJS_HOME
Variable Value: C:\Program Files\nodejs or [Path Of the Nodejs installation directory]

Edit Environment Variable [Path]: Append below line
                                               ;%NODEJS_HOME%\;