Introduction to node.js
By
Dinesh
Agenda
What is node.js?
Why node.js?
How node.js works?
How node.js is different from others?
What can be done with node.js?
Few examples
Who is using node.js?
Why Node.js
what is node.js?
“Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript
engine. Node.js uses an event-driven, Asynchronous non-blocking I/O
model that makes it lightweight and efficient.
What is v8 engine?
● It is a open source javascript engine. Developed by Chromium.
● It is written in c++
● V8 compiles JavaScript to native machine code, thereby compiling
and executing JavaScript code at a lightning speed
Difference between synchronous and asynchronous
● It waits for each
operation to complete,
after that it will
execute next operation.
● Step-By-Step Execution
● It will never waits for
each operation to
complete, rather it
executes all the
operations at a time.
The results of each
operation will be
handled once result is
available.
● CallBacks are used to
handle results
Synchronous example
Example:
var result = database.query("SELECT * FROM hugetable");
console.log(“db operation is done”);
console.log("Hello World");
Output:
db operation is done
Hello World
Asynchronous Example
Example:
database.query("SELECT * FROM hugetable", function(rows) {
var result = rows;
console.log(“After db operation”);
});
console.log("Hello World");
Output:
Hello world
After db operation
Asynchronous Example
Example:
database.query("SELECT * FROM hugetable", function(rows) {
var result = rows;
console.log(“After db operation”);
});
console.log("Hello World");
Output:
Hello world
After db operation
Few more examples...
setTimeout(sayHello,1000);
Function sayHello(){
console.log(“Hello WiCheck Team”);
}
console.log(“VANAKKAM”);
Output:
VANAKKAM
Hello WiCheck Team
How to create callbacks?
function SayHello(str, callback){
//do what ever with str
// finally call callback
callback();
}
sayHello(“Alethea” , function(){
console.log(“I’M CallBack”);
});
Phantomjs
● Headless browser.
var page = require('webpage').create();
page.open('http://www.google.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render(‘google.png');
}
phantom.exit();
});
There is a problem with callbacks....
getData(function(a){
getMoreData(a, function(b){
getMoreData(b, function(c){
getMoreData(c, function(d){
getMoreData(d, function(e){
...
});
});
});
});
});
How to avoid callbacks?
● Promises
● Generators
● Async/wait
● Third party libs(async.js)
Advantages of Node.js
● JavaScript In the backend and frontend
● Best for web servers(http)
● Node.js is a boon for building highly scalable Web App
● Node.js excels at Real-time
applications(Stock,chat,video)
● Data streaming gets easier.
Who uses Node.js
We
NetFlix
NYC
Paypal
Mongodb
LinkedIn
Uber
etc….
Thanks
Questions?

Introduction to node.js

  • 1.
  • 2.
    Agenda What is node.js? Whynode.js? How node.js works? How node.js is different from others? What can be done with node.js? Few examples Who is using node.js?
  • 3.
  • 5.
    what is node.js? “Node.js®is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, Asynchronous non-blocking I/O model that makes it lightweight and efficient. What is v8 engine? ● It is a open source javascript engine. Developed by Chromium. ● It is written in c++ ● V8 compiles JavaScript to native machine code, thereby compiling and executing JavaScript code at a lightning speed
  • 6.
    Difference between synchronousand asynchronous ● It waits for each operation to complete, after that it will execute next operation. ● Step-By-Step Execution ● It will never waits for each operation to complete, rather it executes all the operations at a time. The results of each operation will be handled once result is available. ● CallBacks are used to handle results
  • 7.
    Synchronous example Example: var result= database.query("SELECT * FROM hugetable"); console.log(“db operation is done”); console.log("Hello World"); Output: db operation is done Hello World
  • 8.
    Asynchronous Example Example: database.query("SELECT *FROM hugetable", function(rows) { var result = rows; console.log(“After db operation”); }); console.log("Hello World"); Output: Hello world After db operation
  • 9.
    Asynchronous Example Example: database.query("SELECT *FROM hugetable", function(rows) { var result = rows; console.log(“After db operation”); }); console.log("Hello World"); Output: Hello world After db operation
  • 11.
    Few more examples... setTimeout(sayHello,1000); FunctionsayHello(){ console.log(“Hello WiCheck Team”); } console.log(“VANAKKAM”); Output: VANAKKAM Hello WiCheck Team
  • 12.
    How to createcallbacks? function SayHello(str, callback){ //do what ever with str // finally call callback callback(); } sayHello(“Alethea” , function(){ console.log(“I’M CallBack”); });
  • 13.
    Phantomjs ● Headless browser. varpage = require('webpage').create(); page.open('http://www.google.com', function(status) { console.log("Status: " + status); if(status === "success") { page.render(‘google.png'); } phantom.exit(); });
  • 15.
    There is aproblem with callbacks.... getData(function(a){ getMoreData(a, function(b){ getMoreData(b, function(c){ getMoreData(c, function(d){ getMoreData(d, function(e){ ... }); }); }); }); });
  • 16.
    How to avoidcallbacks? ● Promises ● Generators ● Async/wait ● Third party libs(async.js)
  • 17.
    Advantages of Node.js ●JavaScript In the backend and frontend ● Best for web servers(http) ● Node.js is a boon for building highly scalable Web App ● Node.js excels at Real-time applications(Stock,chat,video) ● Data streaming gets easier.
  • 18.
  • 19.