You are on page 1of 10

Just what is Node.js?

A ready-to-code server

Skill Level: Intermediate

Michael Abernethy
Freelance Programmer
Freelancer

26 Apr 2011

Node is a server-side JavaScript interpreter that changes the notion of how a server
should work. Its goal is to enable a programmer to build highly scalable applications
and write code that handles tens of thousands simultaneous connections on one, and
only one, physical machine.

Introduction
If you've heard about Node, or read any articles claiming how awesome it is, you
may be wondering, "What is Node.js anyway?". Though not for everyone, Node may
be the right choice for some people.

This article will seek to answer what Node.js is by summarizing the problem it can
solve, how it works, how to run a simple application, and finally, when Node is and
isn't a good solution. It will not cover how to write a complicated Node application nor
be a thorough tutorial on Node. Reading this article should help you decide whether
you should pursue learning Node to use in your own business.

What problem does Node solve?


Node's stated goal is "to provide an easy way to build scalable network programs".
What's the issue with current server programs? Let's do the math. In languages like
Java™ and PHP, each connection spawns a new thread that potentially has an
accompanying 2 MB of memory. On a system that has 8 GB of RAM that puts the

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 1 of 10
developerWorks® ibm.com/developerWorks

theoretical maximum number of concurrent connections at about 4,000 users. As


your client-base grows you want your web application to support more users, thus
you have to add more servers. Of course, this adds to a business's costs,
specifically its server costs, traffic costs, and labor costs. Add to those costs the
potential technical issue that a user could use different servers for each request, so
any shared resources have to be shared across all the servers. For example, in
Java, static variables and caches need to be shared across the JVM's on every
server. This is the bottleneck in the entire web application architecture, the maximum
number of concurrent connections a server could handle.

Node solves the problem by changing how a connection is made to the server.
Instead of spawning a new OS thread for each connection (and allocating the
accompanying memory with it), each connection creates a process, which doesn't
require the memory block to go with it. Node claims that it will never deadlock, since
there are no locks allowed, and it doesn't directly block for I/O calls. Node also
claims that a server running it can support tens of thousands of concurrent
connections. In effect, Node changes the server landscape, by changing the
bottleneck in the entire system from the maximum number of connections to the
traffic throughput of one single system.

So, now that you have a program that can handle tens of thousands of concurrent
connections, what can you actually build with Node? It would be awesome if you had
a web application that required this many connections. That's one of those "if you
have this problem, it's not a problem" kind of problems. Before we get to that, let's
look at how Node works and how it's designed to run.

What Node definitely isn't


Yes, Node is a server program. However, it is definitely not like Apache or Tomcat.
Those servers are stand-alone server products, ready to install and deploy
applications instantly. You could be up and running with a server in a minute with
these products. Node is definitely not this. Apache can add a PHP module to allow
developers to create dynamic web pages, and programmers using Tomcat can
deploy JSPs to create dynamic web pages. Node is definitely not this.

At this early point in Node's life (currently on version 0.4.6), it is not a ready-to-run
server program, where you can expect to install it, drop your files into it, and have a
fully functioning web server. It still requires a non-trivial amount of work to get even
the basic functionality of a web server up and running after installation is complete.

How Node works


Node itself runs V8 JavaScript. Wait, JavaScript on the server? Yes, you read
correctly. Server-side JavaScript is a relatively new concept, one which was written

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 2 of 10
ibm.com/developerWorks developerWorks®

about a couple of years ago here on developerWorks when discussing the Aptana
Jaxer product (see Resources). Though Jaxer never really caught on, the idea itself
wasn't that far fetched — why not use the same programming language on the client
that you use on the server?

What's the V8? The V8 JavaScript engine is the underlying JavaScript engine that
Google uses with their Chrome browser. Few people think about what actually
happens with JavaScript on the client. A JavaScript engine actually interprets the
code and executes it. With V8, Google created an ultra-fast interpreter written in C++
that has another unique aspect; you can download the engine and embed it in any
application you wish. It's not restricted to running in a browser. Therefore, Node
actually uses the V8 JavaScript engine written by Google and re purposes it for use
on the server. Perfect! Why create a new language when there's a good solution
already available.

Event-driven programming

Many programmers have been taught to believe that object-oriented programming is


the perfect programming design and to use nothing else. Node utilizes what's called
an event-driven programming model.

Listing 1. Event-driven programming on client-side with jQuery

// jQuery code on the client-side showing how Event-Driven programming works


// When a button is pressed, an Event occurs - deal with it
// directly right here in an anonymous function, where all the
// necessary variables are present and can be referenced directly
$("#myButton").click(function(){
if ($("#myTextField").val() != $(this).val())
alert("Field must match button text");
});

The server-side is actually no different from the client-side. True, there's no pressing
of buttons, and no typing into text fields, but on a higher level, events are taking
place. A connection is made — event! Data is received through the connection —
event! Data stops coming through the connection — event!

Why is this type of setup ideal for Node? JavaScript is a great language for
event-driven programming, because it allows anonymous functions and closures,
and more importantly, the syntax is familiar to nearly everyone who has ever coded.
The callback functions that are called when an event occurs can be written in the
same spot where you capture the event. Thus, it is easy to code, easy to maintain,
with no complicated object-oriented frameworks, no interfaces, and no potential for
over architecting anything. Just listen for an event, write a callback function, and
event-driven programming takes care of everything!

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 3 of 10
developerWorks® ibm.com/developerWorks

Example Node application


Let's finally see some code! Let's put together all the things we've discussed and
create our first Node application. Since we've seen that Node is ideal for handling
high traffic applications, let's create a very simple web application, one built for
maximum speed. Here are the specs for our sample application passed down from
the "boss": Create a random number generator RESTful API. The application should
take one input, a parameter called "number." The application will then return a
random number that's between 0 and this parameter, and return that generated
number to the caller. Because the "boss" expects this to be a massively popular
application, it should handle 50,000 concurrent users. Let's look at the code:

Listing 2. Node random number generator

// these modules need to be imported in order to use them.


// Node has several modules. They are like any #include
// or import statement in other languages
var http = require("http");
var url = require("url");
// The most important line in any Node file. This function
// does the actual process of creating the server. Technically,
// Node tells the underlying operating system that whenever a
// connection is made, this particular callback function should be
// executed. Since we're creating a web service with REST API,
// we want an HTTP server, which requires the http variable
// we created in the lines above.
// Finally, you can see that the callback method receives a 'request'
// and 'response' object automatically. This should be familiar
// to any PHP or Java programmer.
http.createServer(function(request, response) {
// The response needs to handle all the headers, and the return codes
// These types of things are handled automatically in server programs
// like Apache and Tomcat, but Node requires everything to be done yourself
response.writeHead(200, {"Content-Type": "text/plain"});
// Here is some unique-looking code. This is how Node retrieves
// parameters passed in from client requests. The url module
// handles all these functions. The parse function
// deconstructs the URL, and places the query key-values in the
// query object. We can find the value for the "number" key
// by referencing it directly - the beauty of JavaScript.
var params = url.parse(request.url, true).query;
var input = param.number;
// These are the generic JavaScript methods that will create
// our random number that gets passed back to the caller
var numInput = new Number(input);
var numOutput = new Number(Math.random() * numInput).toFixed(0);
// Write the random number to response
response.write(numOutput);
// Node requires us to explicitly end this connection. This is because
// Node allows you to keep a connection open and pass data back and forth,
// though that advanced topic isn't discussed in this article.
response.end();

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 4 of 10
ibm.com/developerWorks developerWorks®

// When we create the server, we have to explicitly connect the HTTP server to
// a port. Standard HTTP port is 80, so we'll connect it to that one.
}).listen(80);
// Output a String to the console once the server starts up, letting us know everything
// starts up correctly
console.log("Random Number Generator Running...");

Starting this application

Put the above code into a file called "random.js". Now, to start this application and
run it (thereby creating the HTTP server and listen for connections on port 80),
simply run the following command in your command prompt: % node random.js.
Here's what it will look like when you know the server is up and running.

root@ubuntu:/home/moilanen/ws/mike# node random.js


Random Number Generator Running...

Accessing this application

The application is up and running. Node is listening for any connections right now,
so let's test the application. Since we've created a simple RESTful API, we can
access the application using our web browser. Type in the following address (make
sure you completed the previous step), http://localhost/?number=27.

Your browser window will change to a random number between 0 and 27. Press
reload on your browser and you'll get another random number. That's it, there's your
first Node application!

Node, what is it good for?


After reading all about Node, you may be able to answer what it is but still wonder
when you should you use it. That's an important question to ask, because there are
certain things that Node is good for, and conversely, there are certain things Node
probably isn't a good solution for right now. You need to decide carefully when to
use Node, because using it in the wrong situation could lead to a LOT of extra
coding.

What it's good for

As you've seen so far, Node is extremely well designed for situations where you are
expecting a high amount of traffic and the server-side logic and processing required
isn't necessarily large before responding to the client. Good examples of where
Node would excel include:

• A RESTful API

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 5 of 10
developerWorks® ibm.com/developerWorks

A web service that provides a RESTful API takes in a few parameters,


interprets them, pieces together a response, and flushes a response
(usually a relatively small amount of text) back to the user. This is an ideal
situation for Node, because you can build it to handle tens of thousands
of connections. It also doesn't require a large amount of logic; it just looks
up values from a database and pieces together a response. Since the
response is a small amount of text, and the incoming request is a small
amount of text, the traffic volume isn't high, and one machine can likely
handle the API demands of even the busiest company's API.

• Twitter queue
Think about a company like Twitter that has to receive tweets and write
them to a database. There are literally thousands of tweets arriving every
second, and the database can't possibly keep up with the number of
writes required during peak usage times. Node becomes an important cog
in the solution to this problem. As we've seen, Node can handle tens of
thousands of incoming tweets. It can write them quickly and easily to an
in-memory queuing mechanism (memcached for example), from which
another separate process can write them to the database. Node's role in
this is to quickly gather the tweet and pass this information off to another
process responsible for writing it. Imagine another design — a normal
PHP server that tries to handle writes to the database itself — every tweet
would cause a small delay as its written to the database since the
database call would be blocking. A machine with this design may only be
able to handle 2000 incoming tweets a second, due to the database
latency. A million tweets per second requires 500 servers. Node, instead,
handles every connection and doesn't block, enabling it to capture as
many tweets as possible. A node machine able to handle 50,000 tweets
per second requires only 20 servers.

• Image file server


A company that has a large distributed website (think Facebook or Flickr),
could decide to devote entire boxes to simply serving up images. Node
would be a good solution for this problem because the company can use
it to code an easy file retriever and then handle tens of thousands of
connections. Node would look for the image file, return it or a 404 error,
and do nothing else. This setup would allow these types of distributed
websites to reduce the number of server boxes they need to serve static
files such as images, .js files, and .css files.

What it's bad for

Of course, Node is not the ideal choice in some situations. Here are scenarios where
Node would not excel:

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 6 of 10
ibm.com/developerWorks developerWorks®

• Dynamically created pages


Currently, Node doesn't provide a default way to create dynamic pages.
For example, when using JavaServer Pages (JSP) technology you can
create an index.jsp page that contains loops in JSP snippets like <% for
(int i=0; i<20; i++) { } %>. Node doesn't enable these types of
dynamic HTML-driven pages. Again, Node isn't ideally suited to be a web
page server, as Apache and Tomcat are. Therefore, if you wanted to
provide a server-side solution for this in Node, you'd have to code the
entire solution yourself. A PHP programmer wouldn't want to program a
PHP converter for Apache every time they deployed a web application,
but at this point, that's what Node would require you to do.

• Relational database heavy applications


Node is designed to be fast, asynchronous, and non-blocking. Databases
don't necessarily share these goals. They are synchronous and blocking,
as calls to the database on writes and reads block until a result is
generated. So, a web application that requires a lot of database calls, a
lot of reads, and a lot of writes with every request would be a bad match
for Node, since the relational database itself would be negating many of
the strengths of Node. (The new NoSQL databases are a better match for
Node, but that's another topic entirely.)

Conclusion
The question "What is Node.js?" should be answered. After reading this article, you
should be able to explain in a few clear and concise sentences what Node.js is. If
you are able to do that, you are ahead of many coders and programmers. Many
people I've talked to about Node have been confused by what it does exactly. They
are, understandably, in the Apache mindset — a server is an application that you
drop your HTML files into and everything works. Node is purpose driven. It's a
software program that uses JavaScript to allow programmers to quickly and easily
create fast and scalable web servers. Where Apache is ready-to-go, Node is
ready-to-code.

Node accomplishes its goals of providing highly scalable servers. It doesn't allocate
a thread-per-connection model, but instead uses a process-per-connection model,
creating only the memory that is required for each connection. It uses an extremely
fast JavaScript engine from Google, the V8 engine. It uses an event-driven design to
keep code minimal and easy-to-read. All of these factors lead to Node's desired goal
— it's relatively easy to write a massively scalable solution.

Just as important as understanding what Node is, it's also important to understand
what it is not. Node is not simply a replacement for Apache that will make your PHP

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 7 of 10
developerWorks® ibm.com/developerWorks

web application more scalable. That couldn't be further from the truth. At this early
stage in Node's life, it has a limited potential for use by many programmers, but in
the situations where it makes sense to use it, it works extremely well.

What should you expect from Node in the future? This is perhaps the most important
question to take away from this article. Now that you know what it does, you should
want to know what it will do next. Within the next year, I look for Node to offer better
integration with existing third party support libraries. Right now, many third party
programmers have developed plugins for Node, including adding file server support
and MySQL support. Look for Node to start integrating that into the core
functionality. Eventually, I also expect Node to support some type of dynamic page
module, whereby you could do the types of things in an HTML file that you can do in
PHP and JSP's (perhaps a NSP, a node server page). Finally, at some point expect
a ready-to-deploy Node server, that you will download and install, and simply drop
your HTML files in to as you can with Apache or Tomcat. It's still early in Node's life,
but it's growing quickly and could be on your horizon soon.

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 8 of 10
ibm.com/developerWorks developerWorks®

Resources
Learn
• The Node.js home page is the launching point for learning about the application.
• Browse the Node.js API page. Note that the syntax may change from release to
release, so check the version you've downloaded against the API you're
browsing.
• Look at Jaxer, the first major attempt to create a server-side JavaScript
environment.
• Read about Aptana Jaxer in the developerWorks article by Michael Galpin.
• Stay current with developerWorks technical events and webcasts.
• Stay current with developerWorks' Technical events and webcasts.
• Follow developerWorks on Twitter.
• Check out upcoming conferences, trade shows, webcasts, and other Events
around the world that are of interest to open source developers.
• Visit the developerWorks Open source zone for extensive how-to information,
tools, and project updates to help you develop with open source technologies
and use them with IBM products.
• Learn about IBM and open source technologies and product functions with the
no-cost developerWorks on-demand demos.
Get products and technologies
• Download Node.js, and download Python, which you will need too.
• See the ever-evolving list of open source software packages on Wikipedia.
• Innovate your next open source development project with IBM trial software,
available for download or on DVD.
Discuss
• Participate in the discussion forum.
• Help build the Real world open source group in developerWorks Community.

About the author


Michael Abernethy

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 9 of 10
developerWorks® ibm.com/developerWorks

In his 13 years in technology, Michael Abernethy has worked with a


wide variety of technologies and a wide variety of clients. He currently
works as a freelance programmer specializing in Java high availability
and jQuery. His focus nowadays is on Rich Internet Applications and
making them both more complex and simpler at the same time. When
he's not working at his computer, he can be found on the golf course, or
more accurately in the bushes, looking for his ball.

Just what is Node.js? Trademarks


© Copyright IBM Corporation 2011 Page 10 of 10

You might also like