nodejs intro part one
DESCRIPTION
Presentation slide for 'Vertis Friday Tech Talk'.TRANSCRIPT
The Server-side JavaScript
Budhram GurungBy -
node.js
Created by Ryan Dahl in 2009
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.
Hello World!!!
file : hello_world.js
console.log('Hello World!!!');
How you can run?
...$ node hello_world.js
Ouput: Hello World!!!
Hurray!!!Crossed most difficult step.
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
Let's code through REPL...
Module
Just a JavaScript file Need to use 'exports' object
to make variable, function reusable
Use 'module.exports' to export whole object
Demo
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.
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:
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
Important
terms
before you know
node.js
Callback
A piece of executable code, that is
passed as an argument to other code
which is expected to execute at
some convenient time.
Example Code
setTimeout(function(){ console.log('World'); }, 1000);
console.log('Hello ');
Output:Hello
......waiting 1 sec.......... World
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!
Event Loop
request
close
Event Queue
Events processed one at a time
CheckingFor
Events
close
connection
request
Known Events
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
Events in node.js
Many objects in Node emit events.
net.Sever
fs.readStream
request
data
EventsEventEmitter
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
Custom EventEmitter Example
Code your properly properly.Event loop main hang or go infinite.
Check Demo
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.
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
Architecture
Runs JavaScript, but isn’tprimarily JavaScript
Node Execution Stack
event_loop()
Node Execution Stack
event_loop()
socket_readable(1)
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
loads('index.html')
Node sends a request to the thread pool to load 'index.html'.
The stack unwinds...
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
loads('index.html')
Node Execution Stack
event_loop()
socket_readable(1)
http_parse(1)
Node Execution Stack
event_loop()
socket_readable(1)
Node Execution Stack
event_loop()
The request is sent to the disk.
In the meantime, someone else connects to the server.
This time requesting an in-memory resource.
Node Execution Stack
event_loop()
Node Execution Stack
event_loop()
socket_readable(2)
Node Execution Stack
event_loop()
socket_readable(2)
http_parse(2)
Node Execution Stack
event_loop()
socket_readable(2)
http_parse(2)
http_respond(2)
Node Execution Stack
event_loop()
socket_readable(2)
http_parse(2)
Node Execution Stack
event_loop()
socket_readable(2)
Node Execution Stack
event_loop()
The process sits idle.
The first request is still hanging.
Eventually the disk responds.
Node Execution Stack
event_loop()
Node Execution Stack
event_loop()
file_uploaded()
Node Execution Stack
event_loop()
file_uploaded()
http_respond(1)
Node Execution Stack
event_loop()
file_uploaded()
Node Execution Stack
event_loop()
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.
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?