Node.js the Right Way
Practical, Server-Side JavaScript That Scales
by Jim R. Wilson
Version: P1.0 (December 2013)
Copyright 2013 The Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Table of Contents
Copyright 2013, The Pragmatic Bookshelf.
Early praise for Node.js the Right Way
Node.js the Right Way really is the right way to get a fast start with modern server-side JavaScript programming. It goes far beyond the basic mechanics of JavaScript and Node and shows you what really goes into making a quality server-side application.
Allen Wirfs-Brock |
Project editor, ECMAScript Language Specification |
If youre just getting started with Node, skip everything else--this is the only book youll need.
Rick Waldron |
Software engineer, Bocoup, LLC |
Finally, a book that teaches that Node.js is much more than a bare-bones web-scale application server for hipsters.
Eric Redmond |
Coauthor of Seven Databases in Seven Weeks |
Node.js the Right Way is a great read that quickly demonstrates Nodes flexibility and power. Its perfect for any JavaScript developer whos interested in exploring the world of server infrastructure.
Node.js the Right Way is the right book to read. Skipping Hello World in favor of applicable examples, Wilson delivers a comprehensive introduction that is detailed yet engaging.
Daniel Renfro |
Lead software engineer at Vistaprint |
This book is a fantastic way to explain Node. I even used some of Jims example code in a personal project (especially Chapter 7).
Mitchell Foley |
Software engineer at Google |
Acknowledgments
This was a surprisingly difficult book to write, and I couldnt have done it without a lot of help.Im especially thankful for my editor, Jackie Carteryour thoughtful feedback made this book what it is today.
Id also sincerely like to thank the whole team at The Pragmatic Bookshelf.Thanks for your kind patience while I figured out how to write this book.And thanks to the entire team, who worked so hard to polish this book and find all of my mistakes.
Id like to thank all my reviewers.Your keen observations have helped make this book even more technically correct (the best kind of correct).In no particular order:
Daniel Rinehart | Gary Katsevman | Xavi Ramirez |
Daniel Renfro | David LaPalomento | Mitch Foley |
Jesse Streb | Jarrett Cruger | Trevor Burnham |
Eric Redmond |
And I want to thank my wonderful family, too.Ruthy, you are my inspiration; with your quiet perseverance, you can achieve anything.Emma and Jimmy, even though youre both growing up too fast, I cant wait to see all the great things youll do.
For anyone I missed, I hope youll accept my apologies.Any omissions were certainly not intentional.
Copyright 2013, The Pragmatic Bookshelf.
Preface
Two big shifts are happening right now in the practice of writing software, and Node.js is at the forefront of both.
First, software is becoming increasingly asynchronous.Whether youre waiting for a Big Data job, interacting with end users, or simply responding to an API call, chances are youll need asynchronous programming techniques.
Second, JavaScript has quietly become the worlds standard virtual machinein web browsers, modern NoSQL databases, and now on the server as well.
Node.js is right at the intersection of these trends, and its ready to take off in a big way.
Why Node.js the Right Way
In March of 2010, I gave a lightning talk titled Full-Stack JavaScript at the NoSQL Boston conference.Back then, and even more so now, I knew that using JavaScript for every layer of the application stack was not only possible, but a great way to reduce software complexity.
The Right Way in this books title refers to both the process of learning Node and the practice of writing Node.
Learning Node.js
As for any growing technology, there are lots of resources available for learning Node.js.Many are intently focused on serving up web resources.The web is great, but its not enough, and its not the whole story of Node.
Ruby is more than Rails, and Python is more than Django.Node.js is more than serving web content, and this book treats it that way.
Node.js the Right Way teaches you the core concepts youll need to be an effective Node.js programmer, no matter what kinds of programs you need to write.
Writing Node.js
One thing I love about JavaScript is that there are seven ways to do anything.Theres breathing room, where developers can explore and experiment and find better approaches to everything.
The community of Node developers, conventions in Node.js development, and even the semantics of the JavaScript language itself are all rapidly evolving.With eyes to the near future, the code examples and recommendations in this book reflect current best practices and standards.
Whats in This Book
This book is for developers who want to learn how to write asynchronous JavaScript for the server using Node.js.Some prior JavaScript experience will help, but you dont have to be an expert.
Chapter 1, , introduces the Node.js event loop, explaining how it empowers Node to be highly parallel and single-threaded at the same time.This chapter also outlines the five aspects of Node.js development that frame each subsequent chapter and has some brief instructions on getting Node installed on your machine.
The remaining chapters each deal with a specific practical programming area.
Wrangling the File System
In Chapter 2, , well get our first look at writing Node.js programs.If youve done any server-side programming in the past, chances are youve had to access a file system along the way.Well start in this familiar domain, using Nodes file-system tools to create asynchronous, nonblocking file utilities.Youll use Nodes ubiquitous