Chapter 2. Getting Started with Node.js

The definition of Node.js that is given on the Node.js website (http://nodejs.org/), is as follows:

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

What matters to us is, Node.js as a part of the platform, provides a scalable and high-performance web application development framework, which allows programming in JavaScript.

Many of us got introduced to JavaScript while building websites or web applications for DOM manipulation, AJAX, and related stuff. But JavaScript is much more than that. Just like C, Java, Python, and so on, JavaScript is also a full-fledged programming language. In all browsers, JavaScript is executed in a virtual machine (VM), in the context of the browser. But it can also be executed in another context—as in the case of a Node.js backend—without the browser.

Node.js uses Google Chrome's JavaScript VM to execute JavaScript applications outside the browser, on the server. Along with this runtime environment, Node.js also provides a library of modules, which provides a framework for building network applications. Node.js is not a web server like the Apache HTTP server, or an application server like Tomcat; but as part of its modules library, Node.js does provide an HTTP Server, which can be used to build web applications.

Apart from having JavaScript as the programming language for the applications, one thing that sets Node.js (and most of the Node.js modules and applications) apart from the traditional servers and applications is the asynchronous event-driven development model, which we will see in later sections.

The origin of Node.js

This is not the first time that JavaScript has been used for server-side programming. Netscape launched Netscape Enterprise Server in 1996, which allowed server-side programming in JavaScript. Since then, many servers, such as RingoJS (http://ringojs.org/), Persevere (http://www.persvr.org/), Mozilla's Rhino-based servers, and others have tried to follow suit.

A major reason for these servers not being taken seriously was the pitiful performance of the JavaScript VMs used by them. JavaScript performance in browsers was also not very good. That was until Google launched its Chrome web browser.

At the time of its launch, Chrome's JavaScript VM, called V8, was almost 10-20 times faster than any other JavaScript VM, and has since then been the fastest.

It was based on this VM that Ryan Dahl developed Node.js in 2008. He wanted to build a server that would enable and empower real-time interactive web applications like Gmail. But Node.js was not the first server he built. Ryan built Ebb, a web server based on Ruby and C, but realized that it wasn't working as fast as he wanted it to. This was followed by several experiments in building a number of small web servers.

Armed with the knowledge gained from his experiments and the study of various platforms, he decided to develop an event-driven or asynchronous server. In the January of 2008, he came up with the idea of building a small web server based on JavaScript. He was inclined towards JavaScript because it was independent of the OS and came without any I/O APIs. He quit his job and worked on Node.js for 6 months. In November 2009, he presented Node.js in JSConf, and has been working for Joyent since then. Initially, Node.js worked only on Unix-based systems; later, it came with support for Windows OS too.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset