Nodejs Intro Part One
-
Upload
budh-ram-gurung -
Category
Education
-
view
2.298 -
download
1
description
Transcript of Nodejs Intro Part One
![Page 1: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/1.jpg)
The Server-side JavaScript
Budhram GurungBy -
![Page 2: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/2.jpg)
node.js
Created by Ryan Dahl in 2009
![Page 3: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/3.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/4.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/5.jpg)
Hurray!!!Crossed most difficult step.
![Page 6: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/6.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/7.jpg)
Let's code through REPL...
![Page 8: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/8.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/9.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/10.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/11.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/12.jpg)
Important
terms
before you know
node.js
![Page 13: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/13.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/14.jpg)
Example Code
setTimeout(function(){ console.log('World'); }, 1000);
console.log('Hello ');
Output:Hello
......waiting 1 sec.......... World
![Page 15: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/15.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/16.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/17.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/18.jpg)
Events in node.js
Many objects in Node emit events.
net.Sever
fs.readStream
request
data
EventsEventEmitter
![Page 19: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/19.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/20.jpg)
Custom EventEmitter Example
![Page 21: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/21.jpg)
Code your properly properly.Event loop main hang or go infinite.
Check Demo
![Page 22: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/22.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/23.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/24.jpg)
Architecture
Runs JavaScript, but isn’tprimarily JavaScript
![Page 25: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/25.jpg)
Node Execution Stack
event_loop()
![Page 26: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/26.jpg)
Node Execution Stack
event_loop()
socket_readable(1)
![Page 27: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/27.jpg)
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
![Page 28: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/28.jpg)
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
loads('index.html')
![Page 29: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/29.jpg)
Node sends a request to the thread pool to load 'index.html'.
The stack unwinds...
![Page 30: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/30.jpg)
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
loads('index.html')
![Page 31: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/31.jpg)
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
![Page 32: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/32.jpg)
Node Execution Stack
event_loop()
socket_readable(1)
![Page 33: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/33.jpg)
Node Execution Stack
event_loop()
![Page 34: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/34.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/35.jpg)
Node Execution Stack
event_loop()
![Page 36: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/36.jpg)
Node Execution Stack
event_loop()
socket_readable(2)
![Page 37: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/37.jpg)
Node Execution Stack
event_loop()
socket_readable(2)
http_parse(2)
![Page 38: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/38.jpg)
Node Execution Stack
event_loop()
socket_readable(2)
http_parse(2)
http_respond(2)
![Page 39: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/39.jpg)
Node Execution Stack
event_loop()
socket_readable(2)
http_parse(2)
![Page 40: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/40.jpg)
Node Execution Stack
event_loop()
socket_readable(2)
![Page 41: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/41.jpg)
Node Execution Stack
event_loop()
![Page 42: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/42.jpg)
The process sits idle.
The first request is still hanging.
Eventually the disk responds.
![Page 43: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/43.jpg)
Node Execution Stack
event_loop()
![Page 44: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/44.jpg)
Node Execution Stack
event_loop()
file_uploaded()
![Page 45: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/45.jpg)
Node Execution Stack
event_loop()
file_uploaded()
http_respond(1)
![Page 46: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/46.jpg)
Node Execution Stack
event_loop()
file_uploaded()
![Page 47: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/47.jpg)
Node Execution Stack
event_loop()
![Page 48: Nodejs Intro Part One](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/48.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/49.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022103016/554a1d63b4c90507558b57ea/html5/thumbnails/50.jpg)