Server Side Javascript Environment
Server Side Javascript Environment
Server Side Javascript Environment
What is node.js?
Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model and designed for writing scalable internet applications, notably web servers. This allows Node.js to get excellent performance based on the architectures of many Internet applications. Programs are written in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability. Node.js is being hailed as the new Ruby on Rails by some in the developer community. But its not a magic bullet, nor is it appropriate for all programming scenarios.
var result = query("select * from users"); for(user in result){ //You get the idea }
In this case the OS thread just sits and waits the I/O operation returns to resume the flow.
Every OS thread takes some memory, so regular servers can't handle many simultaneous connections without penalizing the system performance.
node.js carachteristics
Implemented on top of the V8 js engine (Chromium and Google Chrome engine). Programs are written in plain old javascript, we use the same language for client and server (Die Ruby Die!) Does I/O better:
query("select * from users", function(data, status){ //Do something really cool with users });
Files I/O and db queries are non-blocking Great performance even handling a big number of users It gets better every time google improves V8 It has his own package manager called npm (node package manager)
node.js carachteristics
Nodes Goal
Its goal is to offer an easy and safe way to build high performance and scalable network applications in JavaScript. Those goals are achieved thanks it's architecture:
Single Threaded :
Node use a single thread to run instead of other server like Apache HTTP who spawn a thread per request, this approach result in avoiding CPU context switching and massive execution stacks in memory. This is also the method used by nginx and other servers developed to counter the C10K problem.
Event Loop :
Written in C++ using the Marc Lehmans libev library, the event loop use epoll or kqueue for scalable event notification mechanism.
Nodes Goal
Node has a built-in support for most important protocols like TCP, DNS, and HTTP (the one that we will focus on). The design goal of a Node application is that any function performing an I/O must use a callback. Thats why there is no blocking methods provided in Nodes API. The HTTP implementation offered by Node is very complete and natively support chunked request and response (very useful since we are going to use the twitter streaming api) and hanging request for comet applications. The Nodes footprint for each http stream is only 36 bytes (source).
Alternatives
Ruby Event Machine Python Twisted
Particularly when you are talking about highly-scalable web servers! This is what makes Node.js different and of such interest right now. Add in the fact that it also uses the very common language of JavaScript, and it is a very easy way for developers to create very fast and very scalable servers.
There is no waisted time on the node program, the thread is idle not waiting
What is node.js?
It's a set of tools and libraries living on top of the V8.
Express
Web framework working on top of node.js High Performance Template engines support (jade, ejs) CSS engines support (sass, less, stylus) Partials support
var app = express.createServer(); app.get('/', function(req, res){ res.send('Hello World'); }); app.listen(3000);
Socket I/O
Socket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.
var sys = require("sys"), http = require("http"), url = require("url"), path = require("path"), fs = require("fs"); http.createServer(function(request, response) { var uri = url.parse(request.url).pathname; var filename = path.join(process.cwd(), uri); path.exists(filename, function(exists) { if(!exists) { response.sendHeader(404, {"Content-Type": "text/plain"}); response.write("404 Not Found\n"); response.close(); return; }
fs.readFile(filename, "binary", function(err, file) { if(err) { response.sendHeader(500, {"Content-Type": "text/plain"}); response.write(err + "\n"); response.close(); return; }
response.sendHeader(200); response.write(file, "binary"); response.close(); }); }); }).listen(8080); sys.puts("Server running at http://localhost:8080/");