Posted on by & filed under Content - Highlights and Reviews, Web Development.

JavaScript has certainly seen a big upswing in the past couple of years. In particular, Node.js has caught a lot of attention and has a lot of people talking. Node.js self-describes as an “Event-driven I/O server-side JavaScript environment based on V8.” I’m going to oversimplify that to say it’s something that lets you write web applications in Javascript. I wanted to try my hand at writing an application using Node.js and see what all the fuss was about.

The Namesource App

A couple months back, I did something similar with Phonegap. In that article I pulled together a simple application that would generate some random dudes for use when I’m running a table-top game. I thought I’d take the opportunity to create a companion app. Specifically, the Make A Dude app randomized some names and attributes from a static list.

For this post, I’m going to write an app called Namesource. This app will primarily serve up larger lists of names and attributes using Node.js.

Getting Set Up

I decided to start as simply as possible: Download and install Node.js, and copy out their front page HTTP server example as is:

The example runs perfectly. And I know it should, and I should expect it to, but the whole thing was so easy I’m suspicious. Or jealous. One of those.

Dumping out an object

Now it’s time to take it just a tiny bit further. Rather than outputting Hello World, I want to spit back a JSON array in the same format Make A Dude expected. Something like this:

I can do that pretty simply by just changing the Hello World line to dump out the array as a stringified JSON array.

And if I wanted to dump out a whole bunch of names and attributes, I could wrap everything up in a larger JSON object and just dump that out:

But that approach is hardly ideal. It does the trick, but it means if all I want is a list of names I have to take everything. That doesn’t matter if I have a few of each attribute, but if I a few thousand of each attribute, it’s going to be a lot of stuff to pass around. Instead, I would rather take a look at the request and serve back data that’s being requested.

Using the request

I can look at the request that came in and send only data back that’s in line with that request. For example, I could return the whole dumpme object in response to by doing creating an object to hold responses to different types or requests:

And then calling the relevant property on that object for requests:

Taking that further, I could return only names or other targeted attributes by throwing them into the requestHandler object:

I’m certainly getting closer to something cool, but I’m also setting myself up for having to write (or cut and paste) a lot of code. What if I want only names that start with N? Or what if I want to break my names into categories? Am I could to do all that in my requestHandler object? It seems like someone has probably written a better one.

That’s because someone has written a better one

Node.js has been around for a couple years now, and the odds are that whatever I’m trying to do it’s not new, and it’s not as cool as what someone else did. Already this simple little Namesource app is hinting that it might need a bit of a framework in place. Last time I checked there were 45+ different frameworks available for Node.js, and pages upon pages of modules out there, everything from template engines to content management systems, routers, DB access modules and much more.

I know already that if I want Namesource to be more robust, I’m going to want to rebuild it using one of the available frameworks like Express. But for now I want just try pulling in a module and using it to see what that’s like. I’ll try using the ws WebSockets module and convert this to a WebSockets app.

Moving To WebSockets

I start by using npm (the Node Package Manager, installed as part of Node.js) to install the ws module.

Then I require the pars of the module that I need:

And create a new WebSocketServer object:

Obviously, I need to change a lot of the application I’ve written in order to have it work over WebSockets, but the basic work of getting the module and getting it into my script was dead simple. I stripped down my requestHandler object:

And then I defined some code to respond to requests:

On the other end, I’m doing something like this to request the data:

Astute readers will notice that my requestHandler object is so simple at this point that it isn’t serving a purpose – I could just do something like this:

And if all I’m going to do is dump out the contents of the object unmodified, that’s absolutely true. But if I wanted to ask for something like this:

Then I would still want the requestHandler, so I could do things like this:


Ok, so maybe I didn’t write the coolest, most robust application ever. But I have to admit, I can see what some of the fuss is about. There’s so much available for Node.js right now that it seems like people want to use it to do absolutely everything. I don’t think I’d go that far, but for some applications I can absolutely see the upside.

Safari Books Online has the content you need

Take advantage of these Node.js resources in Safari Books Online:

Node: Up and Running (Rough Cuts) introduces you to Node, the new web development framework written in JavaScript. You’ll learn hands-on how Node makes life easier for experienced JavaScript developers: not only can you work on the front end and back end in the same language, you’ll also have more flexibility in choosing how to divide application logic between client and server.
Read What Is Node? to get up to speed on Node.js with this concise overview.
Node for Front-End Developers shows you how to use this popular JavaScript framework to create simple server applications, communicate with the client, build dynamic pages, work with data, and tackle other tasks.
In Sams Teach Yourself node.js in 24 Hours, expert web developer George Ornbo guides readers through every step of creating custom server-side solutions with Node.js.

About the Author

  Duane O’Brien is a tired computer scientist. He has written a number of articles on developing web applications and various PHP frameworks. To learn more about Duane, check out his blog or read his tweets.

Tags: Javascript, JSON, Node.js, WebSockets,


  1.  Node.js Tip: Why Isn’t this Module Loading? « Safari Books Online's Official Blog
  2.  Node.js Tip: Why Does Node Hate My Object? « Safari Books Online's Official Blog