The document provides an introduction to Node.js, explaining its functionality as a JavaScript runtime built on Chrome's V8 engine and its event-driven, asynchronous model. It contrasts synchronous and asynchronous operations with examples, discusses the challenges of callback management, and introduces alternatives like promises and async/wait. The advantages of Node.js include its suitability for scalable web applications and real-time services, with notable users such as Netflix, PayPal, and Uber.
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?
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
How to createcallbacks?
function SayHello(str, callback){
//do what ever with str
// finally call callback
callback();
}
sayHello(“Alethea” , function(){
console.log(“I’M CallBack”);
});
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.