An Absolute Beginner'S Guide To Node - JS: AUGUST 2014
An Absolute Beginner'S Guide To Node - JS: AUGUST 2014
JS
AUGUST 2014
AN ABSOLUTE BEGINNER’S
GUIDE TO 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 your HTML files. If you want it to be an
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
download page.
$ node
> console.log(‘Hello World’);
Hello World
undefined
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.
hello.js
console.log(‘Hello World’);
$ node hello.js
Hello World
In this example, I moved the console.log message into a file, then passed that file to the node
command as an argument. Node then runs the JavaScript in that file and prints “Hello World”.
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.
my_parser.js
Now let’s add in the parsing. This is pretty much normal JavaScript so I won’t go into any details.
my_parser.js
Now when you pass this file as the argument to the node command it will print the result and exit.
$ node my_parser.js
{ A: 2, B: 14, C: 6 }
ASYNCHRONOUS CALLBACKS
As you saw in the previous example, the typical pattern in Node.js is to use asynchronous callbacks.
Basically you’re telling it to do something and when it’s done, it will call your function (callback). This is
because Node is single-threaded. While you’re waiting on the callback to fire, Node can go off and do
other things instead of blocking until the request is finished.
This is especially important for web servers. It’s pretty common in modern web applications to access
databases. While you’re waiting for the database to return results, Node can process more requests.
This allows you to handle thousands of concurrent connections with very little overhead, compared to
creating a separate thread for each connection.
my_web_server.js
When I say basic, I mean basic. This is not a full-featured HTTP server. It can’t serve any HTML files or
images. In fact, no matter what you request, it will return ‘Hello World’. However, you can run this and
hit [http://localhost:8080] in your browser and you’ll see the text.
$ node my_web_server.js
You might notice something a little different now. Your Node.js application no longer exits. This is
because you created a server and your Node.js application will continue to run and respond to requests
until you kill it yourself.
$ cd /my/app/location
$ npm install express
When you install a module, it will be put in a node_modules folder inside your application directory. You
can now require it like any built-in module. Let’s create a basic static file server using Express.
my_static_file_server.js
You now have a pretty capable static file server. Anything you put in the /public folder can now be
requested by your browser and displayed. HTML, images, almost anything. For example, if you put an
image called “my_image.png” inside the public folder, you can access it using your browser by going
to [http://localhost:8080/my_image.png]. Of course Express has many, many more features, but you
can look those up as you continue developing.
NPM
We touched on npm a little in the previous section, but I want to emphasize how important this tool
will be to normal Node.js development. There are thousands of modules available that solve almost all
typical problems that you’re likely to encounter. Remember to check npm before re-inventing the wheel.
It’s not unheard of for a typical Node.js application to have dozens of dependencies.
In the previous example we manually installed Express. If you have a lot of dependencies, that’s not
going to be a very good way to install them. That’s why npm makes use of a package.json file.
package.json
{
“name” : “MyStaticServer”,
“version” : “0.0.1”,
“dependencies” : {
“express” : “3.3.x”
}
}
Now instead of installing each dependency separately, we can run a single command and install all
of them.
$ npm install
When you run this command npm will look in the current folder for a package.json file. If it finds one, it
will install every dependency listed.
CODE ORGANIZATION
So far we’ve only been using a single file, which isn’t very maintainable. In most applications your code
will be split into several files. There’s no standard or enforced organization to what files go where. This
isn’t Rails. There’s no concept of views go here and controllers go there. You can do what you want.
Let’s re-factor the log parsing script. It’s much more testable and more maintainable if we separate
out the parsing logic into its own file.
parser.js
// Parser constructor.
var Parser = function() {
};
// Parses the specified text.
Parser.prototype.parse = function(text) {
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);
});
return results;
};
// Export the Parser constructor from this module.
module.exports = Parser;
The important piece to this is the “module.exports” line. This tells Node what you’re exporting from this
file. In this case I’m exporting the constructor so users can create instances of my Parser object. You can
export whatever you want.
Now let’s look at how to import this file and make use of my new Parser object.
my_parser.js
Files are included exactly like modules, except you provide a path instead of a name. The .js extension is
implied so you can leave it off if you want.
Since I exported the constructor that is what will be returned from the require statement. I can now
create instances of my Parser object and use it.
SUMMARY
Hopefully this tutorial can bridge the gap between downloading Node.js and building your first widget.
Node.js is an extremely powerful and flexible technology that can solve a wide variety of problems.
I want everyone to remember that Node.js is only bound by your imagination. The core libraries are
very carefully designed to provide the puzzle pieces needed to build any picture. Combine those with
the modules available in npm and it’s amazing how quickly you can begin building very complex and
compelling applications.