Rainbow Coding

Introducing Node.js introduction

Node.js is still fairly a new technology, but it is also very powerful letting you use javascript on server side programming.

Node.js is a technology that has been around for only few years, but it has matured remarkably fast during that time. Big reason of that is probably that Javascript has also risen from nice-to-have to must-have level in web development. This post will show how to get started with node.js and what it can be used for.

Introduction to node.js

Node.js is a JavaScript platform that uses non-blocking I/O model, making it extremely efficient for network applications, I/O heavy applications, and just about anything that requires reponsiveness while doing thousands of other things. As a bonus note, you can re-use any code written in JavaScript. There's a lot of JavaScript around internet.

What this means in practice, is that it is extremely powerful at handling thousands of concurrent tasks simultaneously, and streaming data between files and networks. This is why it makes great server with dynamic requirements and thousands of connections.

While there might be more effective options such as Go or Java, node.js has one more great benefit, which is JavaScript. These days large amounts of code are written in JavaScript for web applications, and being able to use that same code can be considered tremendous benefit. JavaScript's dynamic properties make it a great language for quick prototyping as well as highly scalable requirements.




brew install nodejs


Download and run the installer found in http://nodejs.org/.


There are too many distributions to cover everything, so you should look for your distribution specific instructions. For example in Ubuntu it's simply a matter of running:

apt-get install nodejs

Hello world

Summon your favorite terminal or command-line prompt and run the following:

$ node -e "console.log('Hello world')"
Hello world

The output will be printed on the terminal. Another way to achieve the same thing is to create a file named hello.js and put the following inside:

console.log('Hello world');

After that you can run the program by writing

$ node hello.js
Hello world

As you can imagine, node.js can be used for command-line stuff as easily as Bash/ZSH scripts, ruby, python or anything else.

Node.js for data processing

Node.js is great for data-processing because you can use non-blocking I/O to run other things concurrently while you are doing other things. Let's imagine that we have a data in json and want to convert in a CSV with non-blocking manner.

Let's write the first version with as straightforward manner as possible:

var fruits = [
  { name: "Apple", price: 1 },
  { name: "Orange", price: 0.5 },
  { name: "Banana", price: 0.3 }
  { name: "Grapefruit", price: 1.2 }
for(var fruit in fruits) {

  // do some heavy processing with data
  console.log(fruit.name + '\t' + fruit.price + '\n'); }

Simple solution for simple problem works perfectly when the dataset is small. But if you happen to have 100 000s of lines of data to process, it would block the event loop and all the other activity during the time. Node has couple of possible ways to go around this, such as setTimeout, events, streams, generators and more. Much better way would be to create a stream and stream data there, handle things in batches and so on. In following example we do this with library called async:

var async = require('async'); // to be able to do this, you need to run 'npm install async' first in the directory of the script
var fruits = [
  { name: "Apple", price: 1 },
  { name: "Orange", price: 0.5 },
  { name: "Banana", price: 0.3 }
  { name: "Grapefruit", price: 1.2 }

async.eachSeries(fruits, function (fruit, callback) {

  // do some heavy processing with data
  console.log(fruit.name + '\t' + fruit.price + '\n');


}, function (err) {

  if(err) {

    console.log("TODO: handle errors");



When we run this, we get the exactly same result, but the difference is that other things in our application are not blocked during the processing. Without this kind of approach, if we for example have a web server running, our web server could not be able to accept connections during the processing. More information about async.eachSeries can be found here.


In this tutorial you should have learned how to write very simple programs with node, as well as basics of asynchronous non-blocking I/O.

© RainbowCoding 2010-2021