nodejs intro part one

50
The Server-side JavaScript Budhram Gurung By -

Upload: budh-ram-gurung

Post on 06-May-2015

2.306 views

Category:

Education


1 download

DESCRIPTION

Presentation slide for 'Vertis Friday Tech Talk'.

TRANSCRIPT

Page 1: Nodejs Intro Part One

The Server-side JavaScript

Budhram GurungBy -

Page 2: Nodejs Intro Part One

node.js

Created by Ryan Dahl in 2009

Page 3: Nodejs Intro Part One

Background

node.js runs on V8. It is a set of bindings to the V8 JavaScript VM.

V8 is an open source JavaScript engine developed by Google. Its written in C++ and is used in Google Chrome Browser.

Latest version is v0.10.18

Is Open Source. It runs well on Linux/Unix systems, can also run on Windows systems.

Page 4: Nodejs Intro Part One

Hello World!!!

file : hello_world.js

console.log('Hello World!!!');

How you can run?

...$ node hello_world.js

Ouput: Hello World!!!

Page 5: Nodejs Intro Part One

Hurray!!!Crossed most difficult step.

Page 6: Nodejs Intro Part One

In simple words: Node.js is ‘server-side JavaScript’. In ‘Node.js’ , ‘.js’ doesn’t mean that its solely written JavaScript. It is 40% JS and 60% C++.

A command line tool

A REPL (Read-Eval-Print-Loop)

Introduction

Page 7: Nodejs Intro Part One

Let's code through REPL...

Page 8: Nodejs Intro Part One

Module

Just a JavaScript file Need to use 'exports' object

to make variable, function reusable

Use 'module.exports' to export whole object

Demo

Page 9: Nodejs Intro Part One

node_modules folders

var x_module = require(x);

if x is not : - native module like 'util', 'http' etc - does not begin with '/', './' or '../'

then, check '/node_modules' at parent directory If not found, check '/node_modules' of parent's parent directoryand continue until the root of the tree is reached.

If again not found, throw module not found error.

Page 10: Nodejs Intro Part One

node_modules example

1) /home/ram/projects/node_modules/bar.js

2) /home/ram/node_modules/bar.js

3) /home/node_modules/bar.js

4) /node_modules/bar.js

File at '/home/ram/projects/foo.js' called require('bar.js'), then node would look in the following locations, in this order:

Page 11: Nodejs Intro Part One

npm, short for Node Package Manager

command-line utility for interacting with node.js repository that helps in:

- package installation, - version management, and - dependency management.

Total modules so far: 41,566.

How to install any package/module: ...$ npm install backbone

Page 12: Nodejs Intro Part One

Important

terms

before you know

node.js

Page 13: Nodejs Intro Part One

Callback

A piece of executable code, that is

passed as an argument to other code

which is expected to execute at

some convenient time.

Page 14: Nodejs Intro Part One

Example Code

setTimeout(function(){ console.log('World'); }, 1000);

console.log('Hello ');

Output:Hello

......waiting 1 sec.......... World

Page 15: Nodejs Intro Part One

Blocking IO vs Non-Blocking IO

var result = db.query(“select x from table_Y”);doSomethingWith(result); //wait for result! doSomethingWithOutResult(); //execution is blocked!

Blocking IO

Non-blocking IO

db.query(“select x from table_Y”, function (result){ doSomethingWith(result); //wait for result!

});doSomethingWithOutResult(); // executes without

// any delay!

Page 16: Nodejs Intro Part One

Event Loop

request

close

Event Queue

Events processed one at a time

CheckingFor

Events

close

connection

request

Known Events

Page 17: Nodejs Intro Part One

Events in DOM

The DOM triggers Events.

You can listen for those events.

DOM click

submit

hover

events

When 'click' event is triggered.

$('p').on('click', function(){ .......... });

attach

Page 18: Nodejs Intro Part One

Events in node.js

Many objects in Node emit events.

net.Sever

fs.readStream

request

data

EventsEventEmitter

Page 19: Nodejs Intro Part One

Events in node.js

request: An instance of http.IncomingMessage

response: An instance of http.ServerResponse

net.Server requestemit

function(request, response){ ..... }

When 'request' event is emitted

Event

attach

EventEmitter

Page 20: Nodejs Intro Part One

Custom EventEmitter Example

Page 21: Nodejs Intro Part One

Code your properly properly.Event loop main hang or go infinite.

Check Demo

Page 22: Nodejs Intro Part One

More on node.js

node.js uses an event-driven, non-blocking I/O model, which makes it lightweight. (from nodejs.org!)

Makes use of event-loops via JavaScript’s callback functionality to implement the non-blocking I/O.

Everything inside node.js runs in a single-thread.

Easily building fast, scalable network applications.

Page 23: Nodejs Intro Part One

Focused on performance.

Can handle thousands of concurrent connections with

minimal overhead(CPU/ Memory) on a single process.

Allows one to script programs that do I/O in JavaScript.

More on node.js

Page 24: Nodejs Intro Part One

Architecture

Runs JavaScript, but isn’tprimarily JavaScript

Page 25: Nodejs Intro Part One

Node Execution Stack

event_loop()

Page 26: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(1)

Page 27: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(1)

http_parse(1)

Page 28: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(1)

http_parse(1)

loads('index.html')

Page 29: Nodejs Intro Part One

Node sends a request to the thread pool to load 'index.html'.

The stack unwinds...

Page 30: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(1)

http_parse(1)

loads('index.html')

Page 31: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(1)

http_parse(1)

Page 32: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(1)

Page 33: Nodejs Intro Part One

Node Execution Stack

event_loop()

Page 34: Nodejs Intro Part One

The request is sent to the disk.

In the meantime, someone else connects to the server.

This time requesting an in-memory resource.

Page 35: Nodejs Intro Part One

Node Execution Stack

event_loop()

Page 36: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(2)

Page 37: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(2)

http_parse(2)

Page 38: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(2)

http_parse(2)

http_respond(2)

Page 39: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(2)

http_parse(2)

Page 40: Nodejs Intro Part One

Node Execution Stack

event_loop()

socket_readable(2)

Page 41: Nodejs Intro Part One

Node Execution Stack

event_loop()

Page 42: Nodejs Intro Part One

The process sits idle.

The first request is still hanging.

Eventually the disk responds.

Page 43: Nodejs Intro Part One

Node Execution Stack

event_loop()

Page 44: Nodejs Intro Part One

Node Execution Stack

event_loop()

file_uploaded()

Page 45: Nodejs Intro Part One

Node Execution Stack

event_loop()

file_uploaded()

http_respond(1)

Page 46: Nodejs Intro Part One

Node Execution Stack

event_loop()

file_uploaded()

Page 47: Nodejs Intro Part One

Node Execution Stack

event_loop()

Page 48: Nodejs Intro Part One

Installation

node.js official site: http://nodejs.org/download/

Url: https://github.com/joyent/node/wiki /Installing-Node.js-via-package-manager

sudo apt-get updatesudo apt-get install python-software-properties python g++ makesudo add-apt-repository ppa:chris-lea/node.jssudo apt-get updatesudo apt-get install nodejs=0.10.18-1chl1~precise1

On winodws, you can use node.js installer.

Page 49: Nodejs Intro Part One

Yahoo! : iPad App Livestand uses Yahoo! Manhattan framework which is based on Node.js.

LinkedIn : LinkedIn uses a combination of Node.js and MongoDB for its mobile platform. iOS and Android apps are based on it.

eBay : Uses Node.js along with ql.io to help application developers in improving eBay’s end user experience.

Dow Jones : The WSJ Social front-end is written completely in Node.js, using Express.js, and many other modules.

Complete list can be found at: https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node

Who is using Node.js?

Page 50: Nodejs Intro Part One