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)

What is Node?

Node came about when the original developers took JavaScript, something you could usually only run inside the browser, and they let it run on your machine as a standalone process. This means that we could create applications using JavaScript outside the context of the browser.

Now, JavaScript previously had a limited feature set. When I used it in the browser, I could do things such as update the URL and remove the Node logo, adding click events or anything else, but I couldn't really do much more.

With Node, we now have a feature set that looks much more similar to other languages, such as Java, Python, or PHP. Some of these are as follows:

  • We can write Node applications using the JavaScript syntax
  • You can manipulate your filesystem, creating and removing folders
  • You can create query databases directly
  • You can even create web servers using Node

These were things that were not possible in the past, and they are because of Node.

Now, both Node and the JavaScript that gets executed inside of your browser, they're both running on the exact same engine. It's called the V8 JavaScript runtime engine. It's an open source engine that takes JavaScript code and compiles it into much faster machine code. And that's a big part of what makes Node.js so fast.

Machine code is low-level code that your computer can run directly without needing to interpret it. Your machine only knows how to run certain types of code, for example, your machine can't run JavaScript code or PHP code directly without first converting it into low-level code.

Using this V8 engine, we can take our JavaScript code, compile it to much quicker machine code, and execute that. This is where all those new features come in. The V8 engine is written in a language called C++. So if you want to extend the Node language, you don't write Node code, you write C++ code that builds off of what V8 already has in place.

Now, we'll not be writing any C++ code in this book. This book is not about adding onto Node, it is about using Node. So, we will only be writing JavaScript code.

Speaking of JavaScript code, let's start writing some inside Terminal. Now, throughout the book, we'll be creating files and executing those files, but we can actually create a brand new Node process by running the node command.

Referring to the following screenshot, I have a little right caret, which is waiting for JavaScript Node code, not a new command-prompt command:

This means that I can run something like console.log, which, as you probably already know, logs a message to the screen. log is a function, so I'll call it as such, opening and closing my parentheses, and passing in a string inside two single quotes, a message Hello world!, as shown in the following command line:

console.log('Hello world!');

This will print Hello world to the screen. If I hit enter, Hello world! prints just like you'd expect, as shown in the following code output:

Now, what actually happened behind the scenes? Well, this is what Node does. It takes your JavaScript code, it compiles it into machine code, and executes it. In the preceding code, you can see it executed our code, printing out Hello world!. Now, the V8 engine is running behind the scenes when we execute this command, and it's also running inside the Chrome browser.

If I open up the developer tools in Chrome by going to Settings | More Tools | Developer Tools:

I can ignore most of the things. I'm just looking for the Console tab, as shown in the following screenshot:

The preceding screenshot showing the console is a place where we can run some JavaScript code. I can type the exact same command, console.log('Hello world!'); and run it:

As you can see in the preceding screenshot, Hello world! prints to the screen, which is the exact same result we got when we ran it up earlier using Terminal. In both cases, we're running it through the V8 engine, and in both cases the output is the same.

Now, we already know that the two are different. Node has features such as filesystem manipulation, and the browser has features such as manipulating what's shown inside the window. Let's take a quick moment to explore their differences.

Differences between JavaScript coding using Node and in the browser

Inside the browser, you've probably used window if you've done any JavaScript development:

Window is the global object, it stores basically everything you have access to. In the following screenshot, you can see things such as array, we have all sorts of CSS manipulation and Google Analytics keywords; essentially every variable you create lives inside Window:

We have something similar inside Node called global, as shown here:

It's not called window because there is no browser window in Node, thus it is called global. The global object stores a lot of the same things as window. In the following screenshot, you can see methods that might be familiar, such as setTimeout and setInterval:

If we look at this code screenshot, we have most of the things that are defined inside the window, with some exceptions, as shown in the following screenshot:

Now, inside the Chrome browser, I also have access to document:

The document object stores a reference to the Document Object Model (DOM) in the Node website. The document object shows exactly what I have inside the browser's viewport, as shown in the following screenshot:

I can make changes to the document to update what gets shown up on the browser's viewport. Now, obviously we don't have this HTML document inside Node, but we do have something similar, which is called process. You can view it by running process from Node, and in the following screenshot, we have a lot of information about the specific Node process that's being executed:

There's also methods available here to shut down the current Node process. What I'd like you to do is run the process.exit command, passing in as an argument the number zero, to say that things exited without error:

process.exit(0);

When I run this command, you can see I'm now back at the command prompt, as shown in the following screenshot:

I've left Node, and I'm at a place where I can run any regular command prompt command, such as checking my Node version. I can always get back into Node by running node, and I can leave it without using the process.exit command by using control + C twice.

Now, I'm back at my regular command prompt. So, these are the notable differences, obviously inside the browser you have the viewable area, window gets changed to global, and a document basically becomes process. Now, obviously that's a generalization, but those are some of the big picture changes. We'll be exploring all the minutiae throughout the book.

Now, when someone asks you what is Node? You can say Node's a JavaScript runtime that uses the V8 engine. When they ask you what the V8 engine is, you can say the V8 engine is an open source JavaScript engine written in C++ that takes JavaScript code and compiles it to machine code. It's used inside Node.js and it's used in the Chrome browser.