Justin Chmura
Blarg

Beginner Node.js – Part 2: Node.js Basics

By on

This is the second post in my beginner Node.js series. If you missed part 1, make sure to go back and check it out as this will continue to build on that.

In Part 1, I briefly explained what Node.js is and showed how to get it installed on the 3 major operating systems, Windows, OSX, and Ubuntu. In Part 2, I want to focus more on what Node.js is and what concepts one would need to understand in order to develop effectively using Node.js. Although it is just JavaScript, there are some additional concepts that make JavaScript effective on the server.



Google’s V8 Engine & Start of Node

I’m not talking about Google’s self-driving car, but JavaScriptCore, and Opera/Chrome uses V8. They each do things a little bit different but the end-goal is the same, to turn script into executable code.

When V8 was made open source, Node.js was born. Ryan Dahl, who worked at Joyent at the time, wanted a better way to know the progress of an uploading file from the browser to the server. After that, Node.js has grown into what it is today. If you want to watch a talk about Ryan describing more of Node.js’s history, he gave a talk at a user group that is quite good. I don’t want to go to much more in depth about the beginnings of Node as there’s lots of great material out there that covers it.

Hello World

Now that you know a little about the innards of Node, let’s get back to the code and write our first program. Some of the API’s will look familiar with you’ve done any kind of web JavaScript development. Things like console.log , debugger; , and setTimeout all do similar things in Node.

Assuming you have Node installed, open up a terminal window and type node. This puts you into a Node REPL where you can run code right in the command line. Let’s start by just writing something to the console. Type in console.log(‘Hello World!’); and press enter. This should output Hello World!  and then undefined. The undefined  came from the return value of the console.log.

Node.js REPL

Congrats! You’ve written your first line of Node.js code.

Let’s do the same thing, but run it from a file. Create a hello.js file:

function Person(name) {
  this.name = name;
}

Person.prototype.hello = function () {
  console.log('Hello ' + this.name);
};

var john = new Person('John');
john.hello();

I’m just creating a simple Person class with a method called hello . Creating an instance of that class and calling hello. While being in the same directory as the file, type node hello.js into the command line and you should see ‘Hello John’ as the output.

Require, Exports, & Module.Exports

Those three keywords you will see quite a bit in Node.js projects. If you are familiar with CommonJS on the client-side, then this concept should be familiar. These are used to define a single unit of code, usually in a single file, which can be reused throughout the project.

require  will bring in the functionality you expose in the module. It will also cache the result so if you were to require the same file elsewhere, it doesn’t run the whole file over again. This is useful to know if you need to do some setup before exposing an interface. exports  and module.exports  are fairly similar. They both allow you to expose an interface into your module to the outside world. Taking the person example from above, let’s say I want to move the Person class out to a module in its own file. I’ll create a person.js  file:

function Person(name) {
  this.name = name;
}

Person.prototype.hello = function () {
  console.log('Hello ' + this.name);
};

module.exports = Person;  // This exposes my person class.

Now I refactor my hello.js file:

var Person = require('./person.js');

var john = new Person('John');
john.hello();

Node does cache the value of the module.exports  so if I wanted to use the same Person class in a different file, it doesn’t have to go through the process of creating the Person function again and attaching the prototype methods. This is useful to know if your module needs to do any setup before exposing the interface.

The API

Node has an extensive selection of modules that allow you to get close to the metal. Some of the more popular ones are: http , for handling http requests, fs , for interacting with the file system, and path , for handling and transforming of file paths. If you want to read up on those and the other modules that you can use, the API docs page has them all listed and what functions are available.

Looking to Part 3…

I suggest playing with Node a bit just so you get familiar with creating and bringing in modules. This is a core concept of developing in Node. Also make sure to look at what modules Node makes available to you by default. There’s some powerful things you can do it, for instance, creating a web server, listening to network ports, or processing data over a socket.

The next installment will talk about Node’s Package Manager, or NPM. Node’s built in modules are great, but the real power comes from using packages created by the community.

Stay tuned!