What Is Node JS - Developerworks
What Is Node JS - Developerworks
A ready-to-code server
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.
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.
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.
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
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!
// 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...");
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.
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!
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
• 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.
Of course, Node is not the ideal choice in some situations. Here are scenarios where
Node would not excel:
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
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.
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.