Book Image

Learning Node.js Development

By : Andrew Mead
Book Image

Learning Node.js Development

By: Andrew Mead

Overview of this book

Learning Node.js Development is a practical, project-based book that provides you with all you need to get started as a Node.js developer. Node is a ubiquitous technology on the modern web, and an essential part of any web developers' toolkit. If you are looking to create real-world Node applications, or you want to switch careers or launch a side project to generate some extra income, then you're in the right place. This book has been written around a single goal—turning you into a professional Node developer capable of developing, testing, and deploying real-world production applications. Learning Node.js Development is built from the ground up around the latest version of Node.js (version 9.x.x). You'll be learning all the cutting-edge features available only in the latest software versions. This book cuts through the mass of information available around Node and delivers the essential skills that you need to become a Node developer. It takes you through creating complete apps and understanding how to build, deploy, and test your own Node apps. It maps out everything in a comprehensive, easy-to-follow package designed to get you up and running quickly.
Table of Contents (13 chapters)

Node.js installation

Before we start talking about what Node is and why it's useful, you need to first install Node on your machine, because in the next couple of sections, we'll want to run a little bit of Node code.

Now, to get started, we just need two programs—a browser, I'll be using Chrome throughout the book, but any browser will do, and Terminal. I'll use Spotlight to open up Terminal, which is what it's known as on my operating system.

If you're on Windows, look for the Command Prompt, you can search using the Windows key and then by typing command prompt, and on Linux, you're looking for the command line, although depending on your distribution it might be called Terminal or Command Prompt.

Now, once you have that program open, you'll see a screen, as shown in the following screenshot:

Essentially, it's waiting for you to run a command. We'll run quite a few commands from Terminal throughout the book. I'll discuss it in a few sections later, so if you've never used this before, you can start navigating comfortably.

Node.js version confirmation

In the browser, we can head over to nodejs.org to grab the installer for the latest version of Node(as shown here). In this book, we'll use the most recent version, version 9.3.0:

It is important that you install a V8 version of Node.js. It doesn't have to be 4.0, it could be 1.0, but it is important it's on that V8 branch, because there is a ton of new features that come along with V8, including all of the features you might have come to love in the browser using ES6.

ES6 is the next version of JavaScript and it comes with a lot of great enhancements we'll be using throughout the book. If you look at the following image, Node.js Long Term Support Release Schedule (https://github.com/nodejs/LTS), you can see that the current Node version is V8, out in April 2017:

Before going further, I would like to talk about the Node release cycle. What I have in the preceding image is the official release cycle, this is released by Node. You'll notice that only next to the even Node numbers do you find the active LTS, the blue bar, and the maintenance bar. Now, LTS stands for long-term support, and this is the version that's recommended for most users. I'd recommend that you stick with the currently offered LTS option (Node v 8.9.4 LTS), though anything on the left-hand side will do, this is shown as the two green buttons on nodejs.org.

Now, as you can see, the major version numbers, bump every six months. Regardless of any sort of big overarching change, this happens like clockwork even if nothing drastic has changed. It's not like Angular where jumping from 1.0 to 2.0 was almost like using a completely different library. This is just not the case with Node, what you're getting from this book is the latest and greatest Node has to offer.

Installing Node

Once the version is confirmed and selected, all we have to do is to click the required version button on the Node website (nodejs.org) and download the installer. The installer is one of those basic click Next a few times and you're done type of installers, there's no need to run any fancy commands. I'll start the installer. As shown in the following screenshot, it'll just ask a few questions, then let's click on Next or Continue through all of them:

You might want to specify a custom destination, but if you don't know what that means, and you don't usually do it when installing programs, skip that step too. Here, in the next screenshot, you can see that I'm using just 58.6 MB, no problem.

I'll run the installer by entering my password. And once I enter my password, it should really only take a couple of seconds to get Node installed:

As shown in the following screenshot, we have a message that says The installation was completed successfully, which means we are good to go:

Verifying installation

Now that Node has been installed successfully, we can go ahead and verify that by running Node from Terminal. Inside Terminal, I'll shut it down by going to Quit Terminal and open it up again:

The reason I'm opening it up is because we've installed a new command, and some Terminals require a restart before they will be able to run that new command.

In our case, we restarted things and we can run our brand new command so, we'll type it:

node -v

What we're doing in this command is we're running the Node command, and we're passing in what's called a flag, a hyphen sign followed by a letter. It could be a, it could be j, or in our case it's v. This command will print the version of Node currently installed.

We might get an error like this:

If you try to run a command that doesn't exist, such as nodeasdf, you'll see command not found. If you see this, it usually means the Node installer didn't work correctly, or you haven't run it in the first place.

In our case though, running Node with the v flag should result in a number. In our case, it's version 9.3.0. If you do have Node installed, and you see something like the following screenshot, then you are done. In the next section, we'll start exploring exactly what Node is.