29
Node.JS Introduction Ahmed Elbassel Email: [email protected] Skype: ahmed_elbassel

02 Node introduction

Embed Size (px)

Citation preview

Page 1: 02 Node introduction

Node.JS IntroductionAhmed Elbassel

Email: [email protected]: ahmed_elbassel

Page 2: 02 Node introduction

AgendaStory of Node JsV8 JavaScript EnginePlatform Vs FrameworkWhat is node?What Is Node not?Blocking codeAsynchronous: the browserAsynchronous: the serverEvent LoopModularityInstalling

Page 3: 02 Node introduction

Story of Node Js

Inventor: Ryan DahlRyan Dahl is a math student but he decided to go with programming.

Page 4: 02 Node introduction

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.

Page 5: 02 Node introduction

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.

Page 6: 02 Node introduction

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.

Page 7: 02 Node introduction

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.

Page 8: 02 Node introduction

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.

Page 9: 02 Node introduction

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.

Page 10: 02 Node introduction

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.

Page 11: 02 Node introduction

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

Page 12: 02 Node introduction

What Is Node not?

● It is not a web framework.● For Beginners.

○ It requires more knowledge to work with it.● Multi-threaded.

Page 13: 02 Node introduction

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.

Page 14: 02 Node introduction

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

Page 15: 02 Node introduction

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);

});

Page 16: 02 Node introduction

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.

Page 17: 02 Node introduction

Asynchronous: the server

It’s non-blocking code, it’s it takes only 5 seconds to read both files:

5 seconds

5 seconds

Page 18: 02 Node introduction

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.

Page 19: 02 Node introduction

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.

Page 20: 02 Node introduction

Installing

- Windows- Ubuntu- Mac

Page 21: 02 Node introduction

Installing

Windows:- go to Node website: https://nodejs.org/- download the node version based on your Windows system(32 - 64)

Page 22: 02 Node introduction
Page 23: 02 Node introduction
Page 24: 02 Node introduction
Page 25: 02 Node introduction
Page 26: 02 Node introduction
Page 27: 02 Node introduction
Page 28: 02 Node introduction

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.

Page 29: 02 Node introduction

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.