Está en la página 1de 32

Node.

js

Allows you to build scalable network


applications using JavaScript on the server

What is Node.js?

A JavaScript runtime environment running on


top of Google Chromes V8 JavaScript
Runtime engine
Aka a server-side version of JavaScript
V8 compiles the JavaScript, making its
execution really fast
Runs over the command line, outside the
browser, no GUI interface
Designed for high concurrency multi-tasking
Without threads or new processes
Includes a number of input / output (I/O)
libraries.
The I/O libraries allow node.js to interface
with files or network devices in an

Objective for Node.js


Create a web server using JavaScript
This has been done before: Server-side
JavaScript
- Runs like PHP in the Web Server
- Did not really catch on
- Not what JavaScript was designed for
- JavaScript was slow (and not as
comprehensive) back then
JavaScript Problems
- Performance
- Naming in large systems
- Single-threaded (which is actually good
of node.js!)

Create a simple-to-code, powerful web server


- Designed to scale
- Designed for modern web applications
Enter
Node.js
- Reactive, event-based
- Events for both push- and pull-based
web pages
- Events for what the server needs to do
- Uses JavaScript

The Big Picture


Node.js lets you:
- Create a webserver entirely in JavaScript
- Access it with JavaScript
- Communicate purely with JavaScript objects
client and server

Where does a web app spend its time?


- Listening for requests
- Reading/writing from the network and files
- Accessing a database or outside server
- Not much time is spent doing computation,
rather

waiting!

The above tasks run elsewhere not in the web


app
- Done in the operating system
- Done in database system or application
server
- The web server spends its time waiting for
I/O
But, rather than waiting, why not use nonblocking I/O?

Servers do practically nothing but execute I/O


requests
Network, file store, database
Scripts waiting on I/O requests degrade
performance
I/O operations need to be done differently in web
applications.
- Want responsiveness from the server for client
requests
Servers have to deal with a multitude of client
requests, often simultaneously. Servers need to
multitask to serve these requests.
Conventional servers, e.g. Apache, IIS, have two

Blocking I/O
Many web applications have code like this, i.e.
I/O request, ...
var result = db.query("select * from T");
// use result
What is the software doing while it queries the
In many cases, just waiting for the response
database?
from the database query! Before it can use the
result.
This is called blocking I/O the software
execution is suspended until the I/O completes.
- Bad news! Wastes time.

But a line of code like this


db.query("select..", function (result) {
// use result
});
allows the software to do something else
immediately non-blocking.
This is called non-blocking I/O the software is
not suspended until the I/O completes.
- No wasted time, software more responsive
The above code uses a callback function this
handles the database query result when the I/O
completes, and allows the software to perform
other tasks, instead of blocking.

Callback function

Example

Multi-Threading
Multi-threading is a language feature found in
many modern programming languages, e.g.
Java, C#.
It allows a software process to spawn a copy (or
a partial copy) of itself.
This works best on multi-core processors.

Software process in execution


Spawn two threads

Servers are required to deal with multiple client,


i.e. browser, requests simultaneously, i.e. multitask
the servicing of the requests.
How to achieve multitasking?
Use Multiple threads
- This is what Apache, IIS do
- Threaded coding can be very complex
- And JavaScript does not support threads!
Use Multiple servers
- Need to ensure same browser gets the same
server
- Supported by various front ends for Apache
Multitask without threads
- This what node.js does

all that JavaScript is an event-driven language


It is designed to react to events

de.js makes use of this JavaScript characteristic to impro


performance of servers.

Traditional web-server techniques spawn a new


thread for each connection request from a
browser, eating up system RAM, and eventually
maxing-out at the amount of RAM available,
casing the server to wait for free RAM for other
requests.

Node.js operates on a single-thread (JavaScript


restriction), using non-blocking I/O calls,
allowing it to support tens of thousands of
simultaneous connections.

Web servers (e.g. Apache, IIS) are normally


multithread-based, but node.js is single-thread,
event-based.
Node.js serves each request in a event loop
that is able to handle simultaneous requests.
And too avoid blocking, node.js makes use of the
event-driven nature of JavaScript by attaching
callbacks to I/O requests

JavaScript is Event-Driven
Recall how JavaScript works in the browser
- JavaScript registers for events
( onXXX=function() )
- When something (i.e. an event) happens,
JavaScript is invoked
- The browser continues execution when
JavaScript returns from handling the event.
Node.JS takes this approach
- Start an operation via a function call
- Operation defines a set of events tagged by
name
- Register callbacks (i.e. functions) for events
of interest
- Return control to node.js

The Event Loop

Summary
In a normal process cycle, a web server, while
processing a client request, will wait for I/O
operations to complete, and thus blocks the next
request to be processed.
Node.js, on the other hand, processes each
request as an event.
The server does not wait for an I/O operation
to complete while it can handle other request at
the same time.
When any I/O operation of a request is complete,
it will call-back the server to complete the
request.

Single connection at a time


Synchronous
Synchronous
Synchronous
Synchronous
Synchronous
Synchronous
Synchronous

Single thread

Is also
Synchrono
us

Multiple connections
multithreaded

Asynchronous

Asynchronous

Asynchronous

Scaling node.js
Requires running multiple node.js servers
- On the same machine (multiple cores)
- On separate machines (cluster)
And sending requests based on incoming IP
address
Can be done using node.js

Some Node.js Features


Modules
External libraries are called modules in
node.js.
E.g. Commonly used modules are
fs, http, crypto, os
Imported via the require keyword
E.g.
var http = require("http");
This loads the http library and the single
exported object is available through the http

ode.js Web Server

equire the http module

reate the server function - createServer()

se Request/Response wrapper objects

To write HTTP header statements - writeHead()

To write data for the browser

se listen function (specifies host IP address and port nu


or incoming browser requests.

Example
var http = require('http');
http.createServer( function (req, res) {
res.writeHead(200, {'ContentType':'text/plain'});
res.end('Hello, World!');
}).listen(8080,'127.0.0.1');

Request Wrapper
The Request wrapper
http.IncomingMessage class
Implements the Readable Stream interface
Properties
httpVersion '1.1' or '1.0'
headers object for request headers
method 'GET', 'POST', etc.
url the URL of the request

Response wrapper
The Response wrapper
Implements the Writable Stream interface
Methods
writeHead(statusCode, [headers])
write(chunk, [encoding])
end()
Always call the methods in the following way
writeHead()
write()
end()

También podría gustarte