An Introduction to Node.js

JavaScript has long been the de facto standard for client-side web development. While nearly all client code is written in JavaScript, server-side development is a mashup of PHP, Java, and numerous other technologies. Life as a web developer would be much simpler if a single language was used everywhere. Because JavaScript dominates in the browser, it makes sense to use it on the server as well.

The concept of server-side JavaScript is nothing new. Netscape first introduced JavaScript to the server world in 1994. Since that time, several projects have attempted, and failed, to popularize JavaScript as a server-side language. Performance, or lack thereof, prohibited JavaScript from gaining a real foothold in the server space.

Over the years, JavaScript has seen huge improvements in performance. Due to its relevance in the browser, big players like Google have invested a lot of time and money to make JavaScript as fast as possible. In 2009, Ryan Dahl of Joyent, put all of that newly found performance to good use on the server when he created the Node.js framework. Dahl built Node.js on top of Google’s V8 JavaScript engine. V8 is the same engine that has given Google Chrome its excellent JavaScript performance, and helped it become the most popular browser on the planet.

The Node.js Execution Model

Many servers, such as Apache, maintain a pool of threads for handling multiple concurrent connections. As new connections are established, individual threads are delegated to handle them. Once the request has been serviced, the thread is returned to the pool for reuse by future requests. The problem with this approach is that it lacks scalability. For example, many web applications make AJAX connections which are kept alive indefinitely, consuming the available threads.

Node.js takes a completely different approach to servicing requests. The Node.js execution model is event-driven, and emphasizes asynchronous programming in order to maximize scalability. Node.js also utilizes non-blocking I/O to help conceal its biggest weakness ― the fact that it is single-threaded. Developers must constantly be aware of this limitation, as a single blocking I/O call can potentially grind the entire server to a halt. In the Node.js environment, many functions utilize callback functions which are executed upon completion of I/O events. Some developers find this style of coding to be awkward at first, however it is really no different from handling events on web pages.

Getting Started with Node.js

Node.js is free to use, and can be downloaded from the project’s home page. Binaries and source code are available for Windows, Mac, Linux, and SunOS. Once you have downloaded and installed the framework, you can begin developing Node.js applications.

The simplest “Hello World” Node.js program is shown below. The example uses the built-in console object to display a message in a terminal window. Copy the following code into a new file named “hello.js”.

console.log("Hello World!");

To run the example, type the following command in a terminal window.

node hello.js

If Node.js is configured properly, “Hello World!” will be displayed in the terminal window. Obviously, this is just barely scratching the surface of what Node.js can do. In the coming weeks, we will be diving deeper into the world of Node.js, so be sure to check back!

If you’ve enjoyed this post, you’re going to want to learn all about SitePoint’s newest series of print and ebooks, Jump Start. The first title is Node.js by Don Nguyen — find out more at SitePoint!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • sushil

    can u point in direction or write another blog entry for using node.js in handling requests from browsers and sending responses. this was just too short to start with

    • http://www.cjihrig.com Colin Ihrig

      You’re one step ahead of me. I’m going to be writing regularly about Node.js in the upcoming weeks, so I didn’t want to cover too much at once. Check back this weekend for that exact topic.