I am Hack Sparrow
Captain of the Internets.

Node.js Tutorial

Hello there! I see you wanna learn about Node.js and are looking for a tutorial to help you get productive in it immediately. Great news: you have come to the absolute right place.

There exist many tutorials already on Node.js, written by knowledgeable and skilled people, on many websites. So what’s the point of writing another one? Well, those tutorials, even though informative, do not teach the basics of what Node.js is and the stuff that make it up. Instead of focussing on how to create web servers, this tutorial will show you the truth about Node.js, once you know the truth, and you will be able to do amazing things using Node.js.

Let’s start with what Node.js is

So what is Node.js? A popular answer is “Node.js is JavaScript for the backend”. In my opinion, this answer is so incomplete that, I consider it almost incorrect. That’s like saying, “A human being is a leg”. So what is the correct answer?

Node.js is a platform which enables you to create system level programs using JavaScript. Its built-in libraries allow you to develop all sorts of programs doing all sorts of things - from logging system access to downloading files using the BitTorrent protocol, and what not. If the built-in Node libraries are not enough for what you want to accomplish, you can augment Node using your own C/C++ libraries. In short, Node is a competitor to Bash Script, Perl, Python, C, C++, Java, and other programming languages that might be used to develop system level tools and apps.

A very popular use of Node.js is to develop web servers using its HTTP/HTTPS library. The most prominent example on http://nodejs.org is also a web server, which contributes to the inaccurate notion that “Node.js is backend JavaScript”. Having said that, I understand that the Node guys had to put the web server example on the front page so as to catch the attention of web developers immediately.

Node.js programs, like other programs, can execute on your PC or on a remote server. So Node is not just backend JavaScript, and certainly not a web server.

How do I learn Node.js?

The hardest part of learning Node.js is learning JavaScript. If you already know the core JavaScript language, you are all set to crank out gems after gems of Node apps. If you don’t know, or are not that familiar with JavaScript yet, don’t worry, it’s only a matter of time you became an expert with the right attitude and approach.

Here is a list of resources for those who want to learn more about JavaScript:

1. MDN JavaScript Guide - will teach you the language and its concepts
2. MDN JavaScript Reference - if you want to know more about a JavaScript object

I hope to be able to write a fine JavaScript tutorial for beginners one day, but for now you will have to use your Google-fu to find a JavaScript tutorial for your level, if the docs of MDN are too complicated for you.

Resources for those who are already familiar with JavaScript:

1. CommonJS Modules - because Node.js modules are CommoJS-compliant modules
2. Node.js API - because the API is the tool you will be working with

It is essential you go through all the Node.js libraries because that is how you will get to know what Node is capable of. There are about 34 built-in libraries in Node as of today, you can easily cover all of them in a week’s time.

Writing a Node.js program

We have done a lot of talking so far, maybe it’s time to do some walking.

Watch this thought process of looking up the API doc and creating a Node program:

“Well, well, well. I have the Node.js API page open, and I see this library called File System, let me see what it is all about.”

“Golly molly! That’s a ton of methods on that library. The method names seem to indicate what they might be used for. So how do I use the fs lib?”

Using a Node.js library is pretty easy:

var fs = require('fs');

Now fs would be an instance of the File System library, and you can call any fs method on the instance. Let’s do it with a practical example:

1. Create a directory named `fstest`
2. Under `fstest`, create a file named `app.js`
3. Here is the content for `app.js`

// include the fs lib
var fs = require('fs');
// call readFile method on the fs lib instance to read the content of this file
fs.readFile('app.js', function(err, content) {
// display the file content in the console

4. Execute the file

$ node app.js

You will see some gibberish:

<Buffer 0a 76 61 72 20 66 73 20 3d 20 72 65 71 75 69 72 65 28 27 66 73 27 29 3b 0a 0a 66 73 2e 72 65 61 64 46 69 6c 65 28 27 61 70 70 2e 6a 73 27 2c 20 66 75 6e ...>

Not quite what you expected, aye? That is because the lib is returning the raw buffer, and we are printing it just that.

All objects in JavaScript have a toString() method, which converts the object to UTF string. We convert the raw buffer to a human-readable format by calling toString() on it.

Change: console.log(content);
To: console.log(content.toString());

If you look at the doc, the signature of fs.readFile() method is:

fs.readFile(filename, [encoding], [callback])

That means, fs.readFile() accepts a mandatory filename parameter, an optional encoding type, and a callback function to do stuff with the result of reading the file.

In our example, we omitted the encoding type. What would have happened if we specified the encoding? We would not have required to call toString() on content. Try it:

// include the fs lib
var fs = require('fs');
// call readFile method on the fs lib instance to read the content of this file
fs.readFile('app.js', 'utf8', function(err, content) {
// display the file content in the console

Are you wondering, “How the heck am I supposed to know the possible encoding types”?

I will tell you where to find it this time - it is in the Buffer doc. But learn from this experience, why you should be thorough with the whole of Node.js API docs.

If you can afford it, get the whole Nod.js API doc printed and bound in a hardcopy. Since Node’s release cycle is a couple of weeks, if might make sense to reprint twice or thrice a year.

The second hardest part of learning Node.js is mastering its API. Put a lot of effort in getting to know the API and you will be rewarded aplenty by the god of Node.

“Hey, so can you show a web server example now?”

NO! I don’t want to contribute to the confusion that Node.js is a web server or backend JavaScript. If you want to see a web server example, go to the Node HTTP doc. If you are looking for web development using Node.js, you should check out Express.js, and my Express.js tutorial.

"Why don't you show how to use other APIs of Node.js"

The examples in the API docs are pretty good and is enough for you to know how to start using them. If I cover all the APIs in this tutorial, I would be re-inventing the wheel, and it would take me at least a month to do so. If you know JavaScript, one example is enough example to get you started with Node.js and its modules.

Going beyond Node libraries

The built-in Node.js libraries are the lowest level interfaces available for programming using Node. At times they can get pretty cumbersome to work with. It need to be like that.

*cue Bruce Lee music ...*

Enter Node.js modules. Node modules are Node libraries which exist outside the core Node library set, but work like the built-in libraries. They are mostly written by independent contributors of the Node.js community.

So what does a Node module do?

You want to accomplish something using Node.js? Probably there is a module for that. You want to solve a problem using Node.js? Probably there is a module for that too! Covering the thousands of Node modules are beyond the scope of this tutorial, but I will show you where to find them, how to install them, and how to use them.

Node modules are installed using a utility called NPM (Node Package Manager), which comes along with Node.

Installing a Node module is easy:

$ npm install easyimage

And to use this module in your app:

var easyimg = require('easyimage');

It is that easy and straightforward!

The best place to look for Node modules that might be of your use is located on the wiki page of Node’s GitHub repo - https://github.com/joyent/node/wiki/modules.

And the place to discover and know more about Node modules in general is - https://npmjs.org.

Where do we go from here?

Learning JavaScript, reading up the API docs is not enough to make you a good Node.js developer. You need to actively seek out to further your knowledge and improve your programing skills. The day you think you know enough is the day your obsoletion begins.

For starters, here is a list of things, doing which will help you immensely as a Node.js developer:

1. Subscribe to the official Node.js blog
2. Study the code of some popular Node modules
3. Keep keep yourself updated with every new Node.js release
4. Join the Node.js Google Group
5. Follow these on Twitter - @nodejs, @tjholowaychuk, @izs, @hacksparrow
6. Keep learning JavaScript
7. Subscribe to T. J. Holowaychuk’s blog - dude’s a genius

Parting words

Were you expecting to see a lot of coding examples in this tutorial? Well, you might have. But the nature of Node.js is such that you really don’t need that. JavaScript knowledge, and one good example is all you need to get started with Node.js. Everything else is your reward for having having a curious, and an open mind.

The secret to being a great Node.js developer is to become a JavaScript expert, and to know the API thoroughly.

And always remember, Node.js is not just “backend JavaScript” or a web server, it is a complete system programming platform; backend and web development are just a tiny part of it.

May you be blessed by the FSM and become one of best Node.js developers in the whole world!

Make a Comment