02 node introduction
TRANSCRIPT
Node.JS IntroductionAhmed Elbassel
Email: [email protected]: ahmed_elbassel
AgendaStory of Node JsV8 JavaScript EnginePlatform Vs FrameworkWhat is node?What Is Node not?Blocking codeAsynchronous: the browserAsynchronous: the serverEvent LoopModularityInstalling
Story of Node Js
Inventor: Ryan DahlRyan Dahl is a math student but he decided to go with programming.
Story of Node Js
Once upon a time he was uploading image using flickr, and there is was no status bar.And that was the push to Node JS creation.
V8 JavaScript Engine
V8 is Google's open source JavaScript engine.V8 is written in C++ and is used in Google Chrome, the open source browser from Google.V8 can run standalone, or can be embedded into any C++ application.
Platform VS Framework
What is Software platform?- A software platform is, in the most general sense, whatever pre-existing environment a
piece of computer software or code object is designed to run within, obeying its constraints, and making use of its facilities.
What is a Software framework?- For me, it’s a tool that helps you develop application in a specific way, providing specific
functions you don’t have to do everytime.- Here is an awesome example.
What is Node.js?
Node.js® is a platform built on Chrome's JavaScript runtime 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.
What is Node.js?
Built on JS:The dream of Java write once run everywhere, everyone has it now :)JS is unavoidable, you will write JS code.When AJAX has released. JS started to evolve rapidly.Moreover, because Mozilla, Microsoft, google, opera, started to develop it.
What is Node.js?Node.js uses the Google V8 JavaScript engine to execute code, and a large
percentage of the basic modules are written in JavaScript. Node.js contains a built-in library to allow applications to act as a Web server without software such as Apache HTTP Server or IIS.
What is Node.js?Node.js is gaining adoption as a server-side platform and is used by
Microsoft, Yahoo!, Walmart, Groupon,SAP, LinkedIn, Rakuten, PayPal,Voxer,and GoDaddy.
What is Node.js?Dahl was guided by a few rigid principles:
A Node program/process runs on a single thread, ordering execution through an event loop.
Web applications are I/O intensive, so the focus should be on making I/O fastProgram flow is always directed through asynchronous callbacksComplex programs should be assembled from simpler programs
What Is Node not?
● It is not a web framework.● For Beginners.
○ It requires more knowledge to work with it.● Multi-threaded.
Blocking code
The most common task that any developer would do is File I/O, this being a very fundamental process :
- Open a File.
- Read it's contents and print.
- Do something else.
- For two files, suppose each file needs 5 seconds to be read.
Blocking code
Notes on the previous code:It's a blocking code, until the read operation is completed, the next lines of
code is not executed.It takes 10 seconds to read both files.
5 seconds 5 seconds
Asynchronous: the browser
Node uses event-driven and non blocking I/O model.At the browser:$.get('/getData’, function(response){console.log('my response\t' + response);
});
Asynchronous: the server
It brings the asynchronous model to the server too.Non-blocking code:A waiting process is a wasteful process, especially when waiting for I/O.
Asynchronous: the server
It’s non-blocking code, it’s it takes only 5 seconds to read both files:
5 seconds
5 seconds
Event Loop
What is the Event Loop?The event loop is what allows Node.js to perform non-blocking I/O operations. despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.
Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.
Modularity
In the book The Art of Unix Programming, Eric Raymond proposed the Ruleof Modularity:Developers should build a program out of simple parts connected by well defined interfaces, so problems are local, and parts of the program can be replaced in future versions to support new features. This rule aims to save time on debugging complex code that is complex, long, and unreadable.
Installing
- Windows- Ubuntu- Mac
Installing
Windows:- go to Node website: https://nodejs.org/- download the node version based on your Windows system(32 - 64)
Installing
Ubuntu:sudo apt-get updatesudo apt-get install nodejsThen, Node.js is installed, open a new terminal and write comman
node and press enter.You can write JS code there.
InstallingMac:
- Go to: https://nodejs.org/en/download/- Click Macintosh installer to download- Double click to install
- Then, Node.js is installed, open a new terminal and write command node and press enter.
- You can write JS code there.