Enough Node.js

Just Enough Node.js to Create Basic Command Line Tools, Sites and APIs

I am not an expert at Node.js (hereafter just "Node") but I like it and know enough to get you headed in the write direction. In other words, I know enough Node both to be dangerous and to be useful.

The thing I like the most about Node is that it’s very easy to get started with. If you have done any web programming, you probably already know the language you use (JavaScript) for it but even if you don’t, it is still very easy to approach.

I’m doing this little series for a few reasons. First, I’ve been a bit out of the writing game and need to get started again. Second, I like Node. Third, I told the Dallas HTML 5 Meetup that I would do this for those who couldn’t make part 1 of a two-part talk on getting started with Node and building APIs with it. Fourth, I hope to atone for another post that I wrote on Node three years ago. That post isn’t completely bad...it’s just not the best way to do things. But it did help some people so I’m still glad it is there even if some people pointed out some flaws (and rightly so). It’s just time to write something better :) This series only assumes that you know JavaScript and have a little bit of exposure to web development. If you know more, groovy.

Posts in This Series

These are the posts that I am currently planning on including in the series. I reserve the right to change my mind about any of these posts and most certainly will along the way.

  1. Introducing Node.js (this post)
  2. Enough Node for Building a Basic Command Line Script
  3. Enough Node for Building a Simple Website (with Express)
  4. Creating an API Using Basic REST Principles
  5. Consuming the API Using Angular.js
  6. Consuming the API from an iOS App Built with Swift

What is Node?

Let’s take a quick look at the official description of Node from its home on the web, http://nodejs.org.

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

This is (obviously) a good definition but it would take quite a bit of unpacking and I want to approach it a little differently. I don’t want to give a comprehensive statement about it but want to focus on a few things that are interesting or important to me.

Node is a platform built on Chrome’s JavaScript runtime that allows you build stuff using JavaScript outside of the browser. The vast majority of JavaScript that has been written was meant to run in the browser when viewing a webpage. So the runtime that executes the JavaScript lived in the browser. Node takes the JavaScript runtime that was in Chrome and makes a very fast platform for running JavaScript in other places, whether it is your computer or a server somewhere. This means you can write command-line applications and simple non-web-related scripts in Node. You can use it as an alternative to shell scripts, batch files or other sundry things that you might write to run in a terminal environment. In my time as a developer, I’ve written many one-off scripts to process files or automate something on my machine. Because of Node, I can write those in JavaScript.

Node is an easy-to-approach platform for building web applications. Want to build a website that needs server-side capabilities and you know JavaScript? There are capable and easy-to-use web frameworks that run on Node that make it very simple to create websites.

Node is a single-threaded process that uses event-driven programming and non-blocking I/O to stay fast. Pointing out in our definition that node is a single-threaded process might seem a bit odd at first. We don’t explain that some alternatives like .NET and Java are multi-threadedable processes. But it is absolutely crucial to understanding how Node works so I mention it up front. Having a single thread can be a severe limitation but Node stays fast for most applications by farming out I/O bound tasks (like network calls, database requests, file system operations) to other processes, allowing the Node process to focus on what it is good at, which is handling and processing requests. This makes programming with Node very different than programming with something like ASP.NET. Instead of making a database call and waiting on it to return, a request in Node will use a callback which it will call when the request to the database is finished, allowing the main thread to go back to its business of handling more requests. If you are new to programming and this stuff just went way over your head, don’t worry about it for now.

So that’s Node in a nutshell. Let’s get it installed so we can start tinkering with it.

Installation

Node runs on both Windows and Mac, so let’s take them one at a time.

Windows

Installing Node on Windows is easy. Here are the steps.

  1. Go to http://nodejs.org.
  2. Hit the large button that says "Install". This will download a .msi file.
  3. When that has finished downloading, run the installer. All the default options should be fine.
  4. The installation adds Node to your PATH, so open up a command line and type "node -v" (without the quotes). You should get a version number. If that prints, you have installed node successfully.

And now you are done. You do not need an IDE for development so it doesn’t install one. All you will need is a text editor. Notepad will actually do just fine even if it isn’t the nicest of things to use. You can also use Visual Studio or Web Matrix if you want a more powerful text editor for Node. The tooling on Windows is getting better all of the time these days.

Mac

Installing Node on the Mac is easy. If you are a Homebrew user, all you need to do is type the following in a terminal window:

brew install node

If you aren’t, here are the steps.

  1. Go to http://nodejs.org.
  2. Hit the large button that says "Install". This will download a .pkg file.
  3. When that has finished downloading, run the installer. All the default options should be fine.
  4. The installation adds Node to your PATH, so open up a terminal window and type "node -v" (without the quotes). You should get a version number. If that prints, you have installed node successfully.

And now you are done. You do not need an IDE for development so it doesn’t install one. All you will need is a text editor. Vim, Emacs or the simple TextEdit program that’s included in OSX will do. I am a Sublime Text user and it works great for Node.

Doing The Easiest Possible Thing

Now that we have Node installed, let’s do the easiest possible thing we can do just so we can show ourselves that this is going to work. Open up your command line/terminal window, type this and hit enter.

node -e "console.log('hello world from node.js')"

As you might expect, you get "hello world from node.js" printed to the console. This tells you three things, first that Node is installed (which we knew already), second that the console method that you may be used to using in your browser works here and third that you can run arbitrary JavaScript in your console* with Node. This is cool and kindof a big deal. Normally you won’t be running node with the -e option above because that’s just to tell it that you are going to be passing in some JavaScript that you want evaluated. Normally you will execute a file.

So create a file called "helloworld.js" and paste the console statement into the file. To execute it, use this.

node helloworld.js

Just like before, you get "hello world from node.js" on the console. Since using files is much more convenient than typing all of our JavaScript in a terminal window, we will do that from now on.

Next Steps

Now you have Node installed and know how to run a JavaScript file, we can get started. We will start by using Node to create some basic command line tools and use that as an easy start to using this awesome tool.

comments powered by Disqus