3. What is Node.js? | summary
• is a command-line tool that can be run as a regular web server and
lets one run Javascript programs
• utilizes the very fast V8 Javascript engine built in Google Chrome
• is very good when you need to do several things at the same time
• is event-based so all the wonderful Ajax like stuff can be done on
the server side
• lets us share code between the browser and the backend
Source: http://stackoverflow.com/questions/5062614/how-to-decide-when-to-use-nodejs
4. What is Node.js? | summary
• InfoWorld Technology of the Year Award (2012)
• 2nd most popular repo on Github
5. What is Node.js?
“Node is Javascript on the
server…done right”
“Javascript everywhere”
Sounds confusing:
• Node is not written in Javascript
• It is written in C++, but developers interact with Node
through the look and feel of Javascript
• ~20,000 lines of C++
• ~20,000 lines of Javascript
• Asynchronous server model
6. What is Node.js?
Node is a set of libraries written on top of Chrome‟s V8
Javascript Engine.
What is the V8 Javascript Engine?
• A Javascript engine is software that interprets & executes Javascript.
• V8 Javascript Engine is written in C++ (and so is Node) and is
extremely fast at running Javascript.
7. What is Node.js?
Announced in 2009, founded by Ryan Dahl.
Ryan Dahl originally built Node out of frustrations with
trying to build web servers in Ruby.
8. What is Node.js?
Extremely young.
• 2009: Ryan Dahl announced Node.js
• 2010: became somewhat stable
• 11/2011: “stuff works in windows”
• 6/2012: “stuff works better”
Node is still changing
11. What is Node.js? | I/O models
Blocking I/O Model
Example: ways in which a server can process orders from
customers
Hi, my name is
Apache. How may
I take your order?
• The server serves one
customer at a time.
• As each customer is
deciding on their order, the
server sits and waits.
• When the customer decides
on an order, the server
processes their order and
moves on to the next
customer.
12. What is Node.js? | non-blocking I/O
Blocking I/O Model
Hmm… still
thinking...
OMG she’s blocking
me. I could have
ordered by now.
13. What is Node.js? | non-blocking I/O
Blocking I/O Model
Hmm… still
thinking...
OMG she’s blocking
me. I could have
ordered by now. Pseudocode:
order1 = db.query(“SELECT *
FROM menu WHERE preference =
most”)
order1.process
order2.process
14. What is Node.js? | non-blocking I/O
Blocking I/O Model
The more customers you want
to serve at once, the more
cashier lines you‟ll need.
Cashier lines ~ threads in
computing
Multi-threaded processing
Parallel code execution
Multiple CPUs run at a time,
utilizing shared resources
(memory)
15. What is Node.js? | non-blocking I/O
Non-Blocking I/O Model
I’m still thinking, but
callback to me when
I’m done.While he’s
thinking, I’ll
order the
salmon.
• Node loops through the
customers and polls them to
determine which ones are
ready to order.
• During a function‟s
queue, Node can listen to
another event.
• When the other customer is
finally ready to order, he‟ll
issue a callback.
• Asynchronous callbacks:
“come back to me when I’m
finished”
• function called at the
completion of a given
task.
16. What is Node.js? | non-blocking I/O
Non-Blocking I/O Model
I’m still thinking, but
callback to me when
I’m done.While he’s
thinking, I’ll
order the
salmon.
Node code
console.log(„Hello‟);
setTimeout(function () {
console.log(„World‟);
}, 5000);
console.log(„Bye‟);
// Outputs:
// Hello
// Bye
// World
Allows for high
concurrency
17. What is Node.js? | non-blocking I/O
Non-Blocking I/O Model
I’m still thinking, but
callback to me when
I’m done.While he’s
thinking, I’ll
order the
salmon.
Every function in Node
is non-blocking
Single-threaded
No parallel code
execution
Single CPU
18. What is Node.js? | non-blocking I/O
Node is great for
applications with high
concurrency
(Concurrency =
number of concurrent
clients or users)
19. What is Node.js? | non-blocking I/O
nginx: non-blocking I/O
apache: blocking I/O
23. When should you use Node?
Use Node when:
cost of I/O > cost of more difficult to write code
Cons:
• Not useful for when I/O is light, CPU usage is high (e.g.,
video encoding software)
• Still very barebones, not a powerful framework yet
• You live on Mars
• can‟t utilize any code or libraries in the blocking-I/O world (or else
risk hanging your whole application)
25. Node.js vs Rails
Node.js Ruby on Rails
• Server framework. Requires
Express.js or Mongoose, etc.
• One language: Javascript (and all
of its pros and cons)
• Low-level, barebones framework
with freeform MVC. Requires
custom configuration of modules,
views, and helpers. Handpicking
over default configurations.
• Code sharing between server-side
and client-side JS processes
• Still evolving libraries and tools,
developing ecosystem
• No rapid prototyping
• Web framework
• Ruby, Javascript, SQL
• Full package, many built-in
functions, more out of the box
framework
• “Convention over Configuration” –
spend less time on
configuration, more on actual code
• Utilizes best practices
• Robust sources of libraries and
tools, large ecosystem
• Rapid prototyping
Google V8 Javascript Engine is written in C++Node is a set of libraries on top of V8 to build networking applications.Node has the look and feel of JavascriptNode comes with its own Javascript REPLNode is sugar on top of Google V8
Traditional Way:The server serves one customer at a time.As each customer is deciding on their order, the server sits and waits.When the customer decides on an order, the server processes their order and moves on to the next customer.
Traditional Way:The server serves one customer at a time.As each customer is deciding on their order, the server sits and waits.When the customer decides on an order, the server processes their order and moves on to the next customer.
Traditional Way:The server serves one customer at a time.As each customer is deciding on their order, the server sits and waits.When the customer decides on an order, the server processes their order and moves on to the next customer.
Traditional Way:The server serves one customer at a time.As each customer is deciding on their order, the server sits and waits.When the customer decides on an order, the server processes their order and moves on to the next customer.