top of page

Beginning with Node.js


There's no shortage of Node.js tutorials out there, but most of them cover specific use cases or topics that only apply when you've already got Node up and running. I see comments every once and awhile that sound something like, "I've downloaded Node, now what?" This tutorial answers that question and explains how to get started from the very beginning.

What is Node.js?

A lot of the confusion for newcomers to Node is misunderstanding exactly what it is. The description on nodejs.org definitely doesn't help.

An important thing to realize is that Node is not a webserver. By itself it doesn't do anything. It doesn't work like Apache. There is no config file where you point it to you HTML files. If you want it to be a HTTP server, you have to write an HTTP server (with the help of its built-in libraries). Node.js is just another way to execute code on your computer. It is simply a JavaScript runtime.

Installing Node

Node.js is very easy to install. If you're using Windows or Mac, installers are available on the https://nodejs.org/download/

I've Installed Node, now what?

Once installed you'll have access to a new command called "node". You can use the node command in two different ways. The first is with no arguments. This will open an interactive shell (REPL: read-eval-print-loop) where you can execute raw JavaScript code.

Type in:

$ node

> console.log('Hello World');

In the above example I typed "console.log('Hello World')" into the shell and hit enter. Node will then execute that code and we can see our logged message. It also prints "undefined" because it displays the return value of each command and console.log doesn't return anything.

The other way to run Node is by providing it a JavaScript file to execute. This is almost always how you'll be using it.

hello.js

Type in console.log('Hello World');

Save it then go into terminal and type node hello.js

Your output should be Hello World

Doing Something Useful - File I/O

Running plain JavaScript is fun and all, but not very useful. This is why Node.js also includes a powerful set of libraries (modules) for doing real things. In this first example I'm going to open a log file and parse it.

example_log.txt

2013-08-09T13:50:33.166Z A 2

2013-08-09T13:51:33.166Z B 1

2013-08-09T13:52:33.166Z C 6

2013-08-09T13:53:33.166Z B 8

2013-08-09T13:54:33.166Z B 5

What this log data means is not important, but basically each message contains a date, a letter, and a value. I want to add up the values for each letter.

The first thing we need to do it read the contents of the file.

my_parser.js

Fortunately Node.js makes file I/O really easy with the built-in filesystem (fs) module. The fs module has a function named readFile that takes a file path and a callback. The callback will be invoked when the file is done being read. The file data comes in the form of a Buffer, which is basically a byte array. We can convert it to a string using the toString() function.

Now let's add in the parsing. This is pretty much normal JavaScript so I won't go into any details.

my_parser.js

// Load the fs (filesystem) module.

var fs = require('fs');

// Read the contents of the file into memory.

fs.readFile('example_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 } });

Run to the terminal and now when you pass this file as the argument to the node command it will print the result and exit. $ Node my_parser.js

Your results should be printed! Enjoy!

Recent Posts 
Serach By Tags
No tags yet.
bottom of page