One of the many reasons why I really like working for Towers Watson is being able to expand my knowledge and grow. It has been quite challenging because who am I to be teaching something to some of these very seasoned developers? Regardless I try my best. So I have prepared a presentation on Node.js.

Proceeding further! What is Node.js? Node is a platform build on Chrome's Javascript runtime (V8). It uses event-driven non blocking I/O which makes it lightweight and efficient. Node.js excels in data streaming and I/O operations, but falls short to CPU intensive operations.

Node.js has only been around for about 3 years. It was conceptualized in 2009 making the framework 5 years old. With it being a very young framework, their api and systems are changing constantly. So what are some of the advantages of node.js? Node.js is built upon a common language. There are a lot of developers that know Javascript because it is so common in web development so there shouldn't be anything too unfamiliar when picking up node. Node.js can handle thousands of concurrent connections on 1 process. The models, and services, and validation logic can all be shared between the server and the client. There is also a very large and active community backing this framework.

Now you can't go around just talking about the advantages of some technology without considering some of the disadvantages. As I said before when introducing Node.js; it struggles with intensive CPU operations. Using Node.js as a web-server is also problematic. Not that it can't do it, its just that it doesn't come with all the bells and whistles that Rails, Python or any other web framework comes with. So building a web-server from the ground up isn't uncommon. With it being javascript and with it using a lot of asynchronous operations, the code can be very messy looking. Callback functions are not uncommon and can lead to some unruly code. There is also not a set standard on how and where to put your code. If you should put them in separate files or what the project folder structure should look like. It is currently left up to the developer to decide what works best for them.


Moving onto packages! If you heard of Node.js the thing you have heard the most about it is the Node Package Manager (NPM). There are almost 75,000 packages available to any eager dev. Some of the more popular javascript packages are Express,, and some of my favorites Karma, Grunt and Bower. Using the package manager is pretty simple. If you want to add packages it is a quick

npm install <some-package> [--save-dev]

If you want the package installed globally then you can tack on -g or just leave it off. The default saves the package globally.

What if I already have a package.json file? How do I install the packages then?

npm install

In a little bit we will go over how you can extend upon the packages. For right now I want to take us over some basic examples of the Node.js api. The first file I have is a very simple http server. It will respond to a http request in a browser. Once it gets a request it simply pushes HelloWorld to the client.

var http = require('http');
http.createServer(function(request, response) {
  response.writeHead(200, {'content-Type' : 'text/plain'});
}).listen(8080, '');

console.log("Running Server on");

Simple enough stuff. Now where would we be without some TCP connections? Below I have an example of a server and client connecting on the TCP layer. With this implementation you can send any message to the server and the server will apply some magic then send the message back to the client.

var net = require('net'),
    readline = require('readline');

var server = net.createServer();

server.on('connection', function(socket){
  console.log("Connected! " + socket.remoteAddress);
  socket.write("Welcome " + socket.remoteAddress);
  socket.on('data', function(message){
    console.log('Recieved "' + message + '". Applying mistifications.');
    message = message + "!";

server.listen(8081, '');
Server Code
var net = require('net'),
    readline = require('readline');

var client = new net.Socket();
client.connect(8081, '', function() {
  var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout

  rl.on('line', function(cmd){
    console.log("Sending: "+ cmd);

client.on('data', function(data) {
  console.log('Received: ' + data);

client.on('close', function() {
  console.log('Connection closed');
Client Code

Chat Client

I have created a very basic chat client and published it on github. I have a  few branches that have the information I described above. If you want to try to set it up yourself and walk through the process, the start branch is a good place to start. If you want to see what I have, checkout chat. This is implemented using libraries built on node.js. I am using and Express.

Github notables

Some of the cool things that I showed at the end of my presentation I brought up the power of - I feel - is one of the best examples of the power node.js can offer. In their latest blog post about their 1.0 release they have a few examples of their library at work with emulating a Pokemon(source code) game and a desktop computer. The game I think is really impressive just because it is using javascript to emulate the game and manage all the requests and responses.

Last comments

I am excited to see where this stuff goes. I have a couple of projects that I can see that would benefit having data being streamed into a browser. OR even making an api that is managed by javascript. Even though sometimes javascript can be a little frustrating because of its loosely typed super powers, I find it is very exciting and liberating. Below I have provided my slides, references and my presentation for additional information.


Towers Watson presentation on NodeJs