Learning Node Second Edition Author Name
Learning Node
by Shelley Powers
Copyright 2016 Shelly Powers. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebasto-pol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotion-al use. Online editions are also available for most titles ( http://safaribookson-line.com ). For more information, contact our corporate/institutional sales de-partment: 800-998-9938 or corporate@oreilly.com .
Editor: Meg Foley
Production Editor: FILL IN PRODUCTION EDITOR
Copyeditor: FILL IN COPYEDITOR
Proofreader: FILL IN PROOFREADER
Indexer: FILL IN INDEXER
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest
January -4712: Second Edition
Revision History for the Second Edition
2016-03-15: First Early Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491943052 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Learning Node, the cover image, and related trade dress are trademarks of OReilly Me-dia, Inc.
While the publisher and the author(s) have used good faith eforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual prop-erty rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-94305-2
[FILL IN]
Table of Contents
Preface ix
CHAPTER 1: The Node Environment
Installing Node
Saying Hello to the World with Node
A Basic Hello World Application
Hello World, Tweaked
Node Command-Line Options
Node Hosting Environments
Hosting Node on Your Server, VPS, or Managed Host
Cloud Hosting
The Node LTS and Upgrading Node
Nodes New Semantic Versioning
Upgrading Node
Node, V8, and ES6
Advanced: Node C/C++ Add-ons
CHAPTER 2: Node Building Blocks: the Global Objects, Events, and Nodes Asynchronous Nature
The global and process objects
The global Object
The process Object
Bufers, Typed Arrays, and Strings
Table of Contents
Bufer, JSON, StringDecoder, and UTF-8 Strings
Bufer Manipulation
Nodes Callback and Asynchronous Event Handling
The Event Queue (Loop)
Creating an Asynchronous Callback Function
EventEmitter
The Node Event Loop and Timers
Nested Callbacks and Exception Handling
CHAPTER 3: Basics of Node Modules and Npm
An Overview of the Node Module System
How Node Finds and Loads a Module
Sandboxing and the VM Module
An In-Depth Exploration of NPM
Creating and Publishing Your Own Node Module
Creating a Module
Packaging an Entire Directory
Preparing Your Module for Publication
Publishing the Module
Discovering Node Modules and Three Must-Have Modules
Better Callback Management with async
Command-Line Magic with Commander
The Ubiquitous Underscore
CHAPTER 4: Interactive Node with REPL and More on the Console
REPL: First Looks and Undefined Expressions
Benefits of REPL: Getting a Closer Understanding of JavaScript Under the Hood
Multiline and More Complex JavaScript
REPL Commands
REPL and rlwrap
Custom REPL
Stuf HappensSave Ofen
The Necessity of Console
Console Message Types, Console Class, and Blocking
Table of Contents
Formatting the Message, with Help From util.format() and
util.inspect()
Providing Richer Feedback with console and a Timer
CHAPTER 5: Node and the Web
The HTTP Module: Server and Client
Whats Involved in Creating a Static Web Server
Using Apache to Proxy a Node Application
Parsing the Query with Query String
DNS Resolution
CHAPTER 6: Node and the Local System
Exploring the Operating System
Streams and Pipes
A Formal Introduction to the File System (fs)
The fs.Stats Class
The File System Watcher
File Read and Write
Directory Access and Maintenance
File Streams
Resource Access with Path
Creating a Command-Line Utility
Compression/Decompression with ZLib
Pipes and ReadLine
CHAPTER 7: Networking, Sockets, and Security
Servers, Streams, and Sockets
Sockets and Streams
TCP Sockets and Servers
UDP/Datagram Socket
Guards at the Gate
Setting Up TLS/SSL
Working with HTTPS
The Crypto Module
CHAPTER 8: Child Processes
Table of Contents
child_process.spawn
child_process.exec and child_process.execFile
child_process.fork
Running a Child Process Application in Windows
CHAPTER 9: Node and ES6
Strict Mode
let and const
Arrow Functions
Classes
Promises with Bluebird
CHAPTER 10: Full-stack Node Development
The Express Application Framework
MongoDB and Redis Database Systems
MongoDB
Redis Key/Value Store
AngularJS and Other Full-Stack Frameworks
CHAPTER 11: Node in Development and Production
Debugging Node Applications
The Node Debugger
Node Inspector
Unit Testing
Unit Testing with Assert
Unit Testing with Nodeunit
Other Testing Frameworks
Keeping Node Up and Running
Benchmark and Load Testing with Apache Bench
CHAPTER 12: Node in New Environments
Samsung IoT and GPIO
Windows with Charka Node
Node for Microcontrollers and Microcomputers
Fritzing
Table of Contents
Node and Adruino
Node and Raspberry Pi 2
Preface
Node.js has been around long enough to have achieved adoption by some ma-jor players (LinkedIn, Yahoo!, and Netflix), but is still young enough to be cause for concern from your typical corporate middle manager. Its become a driving force for a more sophisticated JavaScript, as well as the only safe place one can then use the newly improved scripting language. And, since turn-about is fair play, the cutting edge JavaScript has now become the driver for a newly re-vamped Node.js organization and release paradigm.
Node.js has also redefined what we do with JavaScript. Nowadays, an em-ployer is just as likely to demand that JavaScript developers work in a server environment, as well as the more familiar browser. In addition, Node.js has cre-ated a new server language thats generating attention from Ruby, C++, Java, and PHP server-side developers. Especially if these same developers also know JavaScript.
To me, Node.js is fun. Compared to so many other environments, it takes lit-tle efort to get started, to create and host an application, and to try out new things. The scafolding necessary for a Node project just isnt as complex, or pe- dantic, as whats 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 its bits that can be hard to discover. Learning Node.js requires obtaining a mastery of its environment and the core APIs, true. But its also about finding and mastering these hard-to-discover bits.