Exploring Node.js

Gulp and Grunt, which were presented in our previous blog post both use Node.js. After Node.js Interactive Europe in the previous week, which was this year placed in Amsterdam, we decided that we will introduce you to Node.js. Especially that you will be better equipped with knowledge in front of the similar upcoming event in Austin, United States, which will be organized from November 29 to December 2.

It's often wrongly believed that Node.js is a web server. Well, it's not. It doesn't do anything by itself. It doesn't work like Apache. It's a JavaScript runtime built on Chrome's V8 JavaScript engine. So, it is just another way to execute code on your computer. Node.js uses an event-driven, non-blocking I/O model that makes it efficient and lightweight.

Node.js is an open source, cross-platform runtime environment designed to build scalable network applications. Besides that it can also be used for developing server-side applications. But the first one are more often used, because no function in Node directly performs I/O, so the process never blocks. However it is not recommended to use Node.js for CPU intensive applications.

 

Node.js

 

Who uses Node.js?

Node.js also provides a rich library of various JavaScript modules, which simplifies the development of web applications. Therefore a list of projects, companies and applications that uses Node.js is very long. It includes, for example, eBay, General Electric, GoDaddy, PayPal, Uber, Wikipins and Yahoo!.

How to use it

Once you have installed Node.js you will have access to a new command called "node", which you can, for a start, use it with no arguments. That means you can execute raw JavaScript code. In the shell type:

console.log(''Testing new stuff”)

Once you press enter Node executes that code and you will be able to see a logged message which says undefined, because it displays the return value of each command and console.log doesn't return anything. So, practically every time you will be using Node by providing it a JavaScript file to execute. You should first create a js file named test.js on your machine having the following code:

test.js

console.log(“Testing new stuff”);

And then execute test.js file using Node.js interpreter to see the result:

$ node test.js

Testing new stuff

The latter is the result if everything went fine with your installation.

More complex

The above example is plain JavaScript and running it is fun and all, but to be fair, it’s not very useful. Therefore, Node.js also includes a powerful set of modules for doing real things.

Let us, for example, open a log file and parse it.

case_log.txt

2014-08-11T13:50:47.166Z A 2

2014-08-11T13:51:47.166Z B 1

2014-08-11T13:52:47.166Z C 6

2014-08-11T13:53:47.166Z B 8

2014-08-11T13:54:47.166Z B 5

It’s not important what this log data means. The first thing we need to do it read the contents of the file and then add in the parsing.

my_parser.js

// Load the fs (filesystem) module.

var fs = require('fs');//



// Read the contents of the file into memory.

fs.readFile(‘case_log.txt’, function (err, logData) {



// If an error occurred, throwing it will

  // display the exception and kill our app.

  if (err) throw err;



// logData is a Buffer, convert to string.

  var text = logData.toString();



var results = {};



// Break up the file into lines.

  var lines = text.split('\n');



lines.forEach(function(line) {

    var parts = line.split(' ');

    var letter = parts[1];

    var count = parseInt(parts[2]);

   

if(!results[letter]) {

      results[letter] = 0;

    }

   

results[letter] += parseInt(count);

  });



console.log(results);

  // { A: 2, B: 14, C: 6 }

});

When you pass this file to the node command it will print the result. Of course there is a lot more to explore on Node.js, especially regarding modules. One of the them integrates Node.js with Drupal, but we will look at it some other time in one of our next blog posts.