Node.js is a JavaScript runtime built on Chrome's V8 engine that allows JavaScript to be run on the server side. It is single-threaded, non-blocking, and asynchronous which means it relies on callbacks and an event loop to handle concurrent operations like I/O instead of blocking threads. The event loop handles callbacks by placing them in a queue to be executed once the current thread is complete. This allows Node.js to handle a large number of concurrent connections efficiently. Some common uses of Node.js include real-time applications, proxies, and chat applications due to its asynchronous nature. It is not well-suited for heavy computation or large web applications.
2. About me?
NodeXperts?
Premier Software Development Services Company.
Build & Deliver for Node.js.
Tremendous work in :
Angular.js,
D3.js,
Meteor.js,
ReactJs and many more.
4. What is javascript?
JavaScript has “event-loop”, “callback” and a stack.
It is “asynchronous”, “single-threaded” and “non-blocking”.
It uses v8 engine for runtime.
What does it mean?
Everything revolves around “event loop”.
6. Stack Call?
function foo2() {
console.log(“hello world”);
}
function foo1() {
foo2();
}
foo1();
Stack:
Finally: hello world
foo2()
foo1()
main()
7. What happen when we run code synchronously in
single threaded environment?
var request1 =
$.getSync(‘http://xyz.com’);
var request2 =
$.getSync(‘http://xyz1.com’);
console.log(request1);
console.log(request2)
What will happen?
1.request1 will process, then
wait for the response.
2.Request2 will process, again
wait for response.
Why? Coz thread is busy processing
that request.
8. Then, how Javascript works asynchronously?
The answer lies in event loop?
What this event loop is?
Let’s see...
9. What is Node.js then?A server side v8 engine
I hope you now have a basic idea of how javascript
works on browser?
10. What? Now we can run javascript on server side.
Yes, it allows us to run JavaScript code in the backend,
outside a browser.
How? Google's V8 VM.
Comes with two things: a runtime environment and a library
(NPM).
Sounds awesome, right?
11. Features of Node Js
Relies on asynchronous code to stay fast and non-blocking.
Non-blocking
Blocking:
console.log("Hi!");
for(var i=0; i<100; i++) {
console.log("Inside the loop");
}
console.log("Welcome to NodeXperts.");
13. Let’s take a look at an example
var fs = require('fs');
fs.readFile('nx.txt', function (err, data){
if(err) throw err;
console.log(data);
})
14. While there are events to process:
e = get the next event
Perform the action requested by e in thread
If e’s thread is complete and there is a callback associated with e:
Call the callback
16. Where you can use Node Js?
Few CPU cycles
I/O operations
Chat Applications
Proxies
17. Where you can not use Node Js?
Heavy computation.
Large and complicated web application because it doesn’t
support multi-threaded programming.
18. Advantages of Node Js
Open source
JavaScript language is used on both front-end & back-end
High Scalability both Horizontally & Vertically
Better Performance than traditional web servers
Has a wide community of developers around the world