Preface

Node.js has been around long enough to have achieved adoption by some major players (LinkedIn, Yahoo!, and Netflix), but is still young enough to be cause for concern for your typical corporate middle manager. It’s been a driving force behind creating a more sophisticated JavaScript, as well as the only safe place one can then use the newly improved scripting language. And, since turnabout is fair play, the cutting-edge JavaScript has now become the driver for a newly revamped Node.js organization and release paradigm.

Node.js has also redefined what we do with JavaScript. Nowadays, an employer is just as likely to demand that JavaScript developers work in a server environment as well as the more familiar browser environment. In addition, Node.js has created a new server language that’s generating attention from Ruby, C++, Java, and PHP server-side developers—especially if they also know JavaScript.

To me, Node.js is fun. Compared to so many other environments, it takes little effort to get started, create and host an application, and try out new things. The scaffolding necessary for a Node project just isn’t as complex or pedantic as what’s required for other environments. Only PHP has as simple an environment, and even it requires tight integration with Apache to create outward-facing applications.

As simple as it is, though, Node.js has bits that can be hard to discover. It is true that learning Node.js requires obtaining a mastery of its environment and the core APIs, but it’s also about finding and mastering these hard-to-discover bits.

Who This Book Is For

I see two audiences for this book.

The first audience is the developer who has been creating frontend applications using a variety of libraries and frameworks, and who now wants to take their JavaScript skills to the server.

The second audience is the server-side developer who wants to try something new or needs to make a shift to a newer technology. They’ve worked with Java or C++, Ruby or PHP, but now they want to take the JavaScript they’ve picked up over time, and their knowledge of the server environment, and merge the two.

These are two seemingly separate audiences with one shared knowledge: JavaScript, or ECMAScript if you want to be more precise. This book does require that you are very comfortable working with JavaScript. Another commonality is that both audiences need to learn the same Node basics, including the core Node API.

However, each audience brings a different perspective, and skills, to the learning experience. To enhance the usefulness, I’ll work to incorporate both perspectives into the material. For instance, a C++ or Java developer might be interested in creating C++ Node add-ons, which might not be interesting to the frontend developer. At the same time, some concepts like big-endian may be very familiar to the server-side developer but unknown to the frontend person. I can’t dive too deeply into either viewpoint, but I will work to ensure that all readers don’t end up frustrated or bored.

One thing I’m not going to do is force rote memorization on you. We’ll get into the core module APIs, but I’m not going to cover every object and function, because these are documented at the Node website. What I’ll do instead is touch on important aspects of each core module or specific Node functionality I think is essential in order to, hopefully, give you a baseline so you can hold your own with other Node developers. Of course, practice makes mastery, and this book is a learning tool. Once you’re finished with the book you’ll need to continue on for more in-depth explorations of specific types of functionality, such as working the Mongo-Express-Angular-Node (MEAN) stack. This book will give you the grounding you need to branch out in any of the many Node directions.

Node.js Is Node

The formal name is Node.js, but no one uses it. Everyone uses “Node.” End of story. And that’s what we’ll be using, for the most part, in this book.

Book Structure

Learning Node is a back-to-basics book. Its focus is on Node, and the modules that make up the Node core. I do lightly touch on some third-party modules, and provide extensive coverage of npm, of course, but the primary goal of this book is to bring you, the reader, up to speed on basic Node functionality. From this solid platform, you can then move in other directions.

Chapter 1 includes an introduction to Node, including a brief section on how to install it. You’ll also get a chance to take Node out for a spin, first by creating the web server listed in the Node documentation, and then by creating a bit more advanced server using the code I provide. In addition, I’ll also touch on creating Node add-ons, for the C/C++ coders among you. And what would an introduction to Node be without an overview of the history of this environment, which issued its first release as 4.0 rather than 1.0.

Chapter 2 covers essential Node functionality including how events are handled, the global objects underlying the necessary functionality to create Node applications, and the asynchronous nature of Node. In addition, I also cover the buffer object, which is the data structure transmitted via most network services in Node.

Chapter 3 dives into the nature of Node’s module system, as well as providing an in-depth look at npm, a Node third-party module management system. In this chapter, you’ll discover how your application finds the Node modules you’re using, and how you can create a Node module of your own to use. Lastly, I get into the more advanced aspects of Node functionality by exploring the support for sandboxing. Just for fun, I also cover three popular Node third-party modules: Async, Commander, and Underscore.

The interactive console that comes with Node, and which goes by the name of REPL, is an invaluable learning tool and environment, and it gets its own chapter in Chapter 4. I cover how to use the tool, in detail, as well as how to create your own custom REPL.

We explore developing Node applications for the Web in Chapter 5, including taking a much closer look at the Node modules that support web development. You’ll get a chance to see what it takes to create a fully functional static web server, as well as learning how you can run a Node application alongside Apache, via the use of an Apache proxy.

Node works in a variety of environments, including both Windows and Unix-based systems such as OS X and Linux. It does so by providing a set of functionality that normalizes system differences, and this functionality is explored in Chapter 6. In addition, I also cover the fundamental nature of Node streams and pipes—essential elements of all input/output—as well as exploring Node’s filesystem support.

Chapter 7 is all about networks, and you can’t touch on networking without also touching on security. The two should go hand-in-hand, like peanut butter and jelly, or chocolate with anything. I’ll introduce you to Node’s TCP and UDP support, as well as cover how to implement an HTTPS server, in addition to the HTTP server you learned how to create in Chapter 5. I’ll also cover the mechanics behind digital certificates and the fundamentals of Secure Sockets Layer (SSL), and its upgrade, Transport Layer Security (TLS). Lastly, we’ll look at the Node crypto module and working with password hashes.

One of my favorite aspects of Node is its ability to work with operating system functionality via child processes. Some of my favorite Node applications are small utility programs for working with compressed files, using the popular graphics application, ImageMagick, and creating a screenshot application for grabbing screenshots from websites. They’re not big-time applications with sophisticated cloud interfaces, but they are a fun way to learn to work with child processes. Child processes are covered in Chapter 8.

Most of the examples in the book use the same JavaScript you’ve been using for years. However, one of the main impetuses behind the Node.js/io.js split and the new, merged product is support for the newer ECMAScript versions, such as ES6 (or ECMAScript 2015, if you prefer). In Chapter 9, I cover what’s currently supported in Node, the impact of the new functionality, and when and why to use new functionality over old. I also cover the gotchas related to using the newer JavaScript capabilities. The only time I won’t focus on the native functionality is when I cover promises as implemented by that very popular module, Bluebird.

Chapter 10 takes a look at the frameworks and functionality that make up what is known as full-stack Node development. We’ll take a look at Express, a now ubiquitous component of most Node development, as well as try out both MongoDB and Redis. We’ll also explore a couple of the frameworks that make up the “full stack” in full-stack Node Development: AngularJS and Backbone.js.

Once you’ve coded your Node application, you’ll want to push it to production. Chapter 11 covers tools and techniques for Node development and production support, including unit, load, and benchmark testing, as well as essential debugging skills and tools. You’ll also learn how to run your Node application forever, and restore it if it fails.

Chapter 12 is dessert. In this chapter, I’ll introduce you to the ways of taking your mad Node skills to new worlds, including Node in microcontrollers/microcomputers, as a part of the Internet of Things, and a version of Node that does not run on V8.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/shelleyp/LearningNode2.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Learning Node by Shelley Powers (O’Reilly). Copyright 2016 Shelley Powers, 978-1-4919-4312-0.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/learning-node-2e.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

I would like to thank the folks who helped me pull this book together: editor, Meg Foley; tech reviewer, Ethan Brown; copyeditor, Gillian McGarvey; proofreader, Rachel Monaghan; indexer, Judy McConville; illustrator, Rebecca Panzer; and anyone else who has touched this book!

Get Learning Node, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.