Before you commit to reading this book, you should know what it doesn’t cover.
At the time of this writing, npm houses more than 528,000 modules, with a growth rate of more than 500 new modules per day.[1] Since the ecosystem and community around Node.js is so large and still growing so rapidly, this book does not attempt to cover everything. Instead, this short book teaches you the essentials you need to get out there and start coding.
In addition to the wealth of Node.js modules available, there’s the added complexity of working with non-Node.js services and platforms. Your Node.js code will invariably act as an intermediary between various systems and users both up and down the stack. To tell a cohesive story, we’ll naturally only be able to dive deep on a few of these, but always with an eye to the bigger picture.
If you’re looking for an opinionated book that focuses only on a particular stack like MEAN (Mongo, Express, Angular, and Node.js), this is not it! Rather than prescribe a particular stack, I’ll teach you the skills to put together the Node.js code, no matter which back end you connect to or front end you choose to put on top.
Instead of MongoDB, I’ve selected Elasticsearch to back the projects in this book because it’s increasingly popular among experienced Node.js developers, as evidenced by a 2016 survey by RisingStack.[2] Moreover, with its REST/JSON API, Elasticsearch offers a way to ease into HTTP services as a consumer before jumping into writing your own.
This book also shies away from front-end JavaScript frameworks. The two most popular front-end frameworks at the time of this writing are React, by Facebook,[3] and Angular, by Google.[4] This book covers neither of them in detail, by design. They both deserve more coverage than fits in these pages.
I want you to be the best Node.js coder you can be, whether you use any particular database or front-end framework.
The JavaScript language is probably the most misunderstood language today. Although this book does discuss language syntax from time to time (especially where it’s brand-new), this is not a beginner’s guide to JavaScript. As a quick quiz, you should be able to easily read and understand this code:
| | const list = []; |
| | for (let i = 1; i <= 100; i++) { |
| | if (!(i % 15)) { |
| | list.push('FizzBuzz'); |
| | } else if (!(i % 5)) { |
| | list.push('Buzz'); |
| | } else if (!(i % 3)) { |
| | list.push('Fizz'); |
| | } else { |
| | list.push(i); |
| | } |
| | } |
You may recognize this as a solution to the classic programming puzzle called FizzBuzz, made famous by Jeff Atwood in 2007.[5] Here’s another solution—one that makes gratuitous (and unnecessary) use of some of the newer JavaScript features.
| | 'use strict'; |
| | const list = [...Array(100).keys()] |
| | .map(n => n + 1) |
| | .map(n => n % 15 ? n : 'FizzBuzz') |
| | .map(n => isNaN(n) || n % 5 ? n : 'Buzz') |
| | .map(n => isNaN(n) || n % 3 ? n : 'Fizz'); |
If you don’t recognize the techniques used in this code, that’s expected! You’ll learn to use several of them, and many others, in this book.
The examples in this book assume you’re using a Unix-like operating system. We’ll make use of standard input and output streams, and pipe data between processes. The shell session examples have been tested with Bash, but other shells may work as well.
If you run Windows, I recommend setting up Cygwin.[6] This will give you the best shot at running the example code successfully, or you could run a Linux virtual machine.