3. 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.
4. Hello World!!!
file : hello_world.js
console.log('Hello World!!!');
How you can run?
...$ node hello_world.js
Ouput:
Hello World!!!
6. 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
8. Module
Just a JavaScript file
Need to use 'exports' object
to make variable, function reusable
Use 'module.exports' to export
whole object
Demo
9. 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 directory
and
continue until the root of the tree is reached.
If again not found, throw module not found error.
10. 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:
11. 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
15. 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!
17. 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
18. Events in node.js
Many objects in Node emit events.
net.Sever
fs.readStream
request
data
EventsEventEmitter
19. Events in node.js
request: An instance of http.IncomingMessage
response: An instance of http.ServerResponse
net.Server request
emit
function(request, response){ ..... }
When 'request' event is emitted
Event
attac
h
EventEmitter
21. Code your properly properly.
Event loop main hang or go infinite.
Check Demo
22. 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.
23. 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
48. 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 update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs=0.10.18-1chl1~precise1
On winodws, you can use node.js installer.
49. 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?