2. 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?
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 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
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
12. 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â);
});
15. 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){
...
});
});
});
});
});
16. How to avoid callbacks?
â 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.