node.js
DESCRIPTION
Introduction to node.jsTRANSCRIPT
A JavaScript runtime environment running on top of Google Chrome’s 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 asynchronous, non-blocking manner. This allows node.js to be used as a fast, lightweight, event-
based web server. Never blocks, not even for I/O
What is Node.js?
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 non-blocking I/O?- When I/O finishes, tell the server and let it process the result- Multiple I/O operations can be active at once- Other operations can be treated as I/O, i.e. as events
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 performance problems when dealing with the multitude of client requests
- Multi-threading- Blocking I/O
In many cases, just waiting for the response 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.
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 database?
Blocking I/O
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.
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.
Spawn two threads
Software process in execution
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
Recall that JavaScript is an event-driven language
- It is designed to react to “events”
Node.js makes use of this JavaScript characteristic to improvethe 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
- Node.js will run the operation in background- And invoke your functions as needed
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.
Synchronous
Synchronous
Synchronous
Synchronous
Synchronous
Synchronous
Synchronous
Single connection at a time
Single thread
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
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 variable.
Some Node.js Features
A Node.js Web Server
1. require the http module
2. Create the server function - createServer()
3. Use Request/Response wrapper objects
- To write HTTP header statements - writeHead()
- To write data for the browser
4. Use listen function (specifies host IP address and port number)for incoming browser requests.
var http = require('http');
http.createServer( function (req, res) { res.writeHead(200, {'Content-Type':'text/plain'}); res.end('Hello, World!');
}).listen(8080,'127.0.0.1');
Example
• 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
Request Wrapper