Table of Contents
Preface vii
1 The Node Environment 1
Installing Node 2
Saying Hello to the World with Node 3
A Basic Hello World Application 3
Hello World, Tweaked 6
Node Command-Line Options 10
Node Hosting Environments 11
Hosting Node on Your Server, VPS, or Managed Host 11
Cloud Hosting 11
The Node LTS and Upgrading Node 13
Node's New Semantic Versioning 13
Upgrading Node 14
Node, V8, and ES6 16
Advanced: Node C/C++ Add-ons 17
2 Node Building Blocks: Global Objects, Events, and Node's Asynchronous Nature 19
The global and process Objects 20
The global Object 20
The process Object 21
Buffers, Typed Arrays, and Strings 25
Buffer, JSON, StringDecoder, and UTF-8 Strings 28
Buffer Manipulation 30
Node's Callback and Asynchronous Event Handling 33
The Event Queue (Loop) 33
Creating an Asynchronous Callback Function 36
EventEmitter 39
The Node Event Loop and Timers 43
Nested Callbacks and Exception Handling 46
3 Basics of Node Modules and Node Package Manager (npm) 55
An Overview of the Node Module System 55
How Node Finds and Loads a Module 56
Sandboxing and the VM Module 59
An In-Depth Exploration of NPM 63
Creating and Publishing Your Own Node Module 69
Creating a Module 69
Packaging an Entire Directory 70
Preparing Your Module for Publication 71
Publishing the Module 75
Discovering Node Modules and Three Must-Have Modules 76
Better Callback Management with Async 77
Command-Line Magic with Commander 83
The Ubiquitous Underscore 84
4 Interactive Node with REPL and More on the Console 87
REPL: First Looks and Undefined Expressions 87
Benefits of REPL: Getting a Closer Understanding of JavaScript Under the Hood 89
Multiline and More Complex JavaScript 90
REPL Commands 94
REPL and rlwrap 95
Custom REPL 96
Stuff Happens-Save Often 100
The Necessity of the Console 100
Console Message Types, Console Class, and Blocking 100
Formatting the Message, with Help from util.format() and util.inspect() 103
Providing Richer Feedback with console and a Timer 107
5 Node and the Web 109
The HTTP Module: Server and Client 109
What's Involved in Creating a Static Web Server 114
Using Apache to Proxy a Node Application 124
Parsing the Query with Query String 125
DNS Resolution 126
6 Node and the Local System 129
Exploring the Operating System 129
Streams and Pipes 131
A Formal Introduction to the File System 133
The fs.Stats Class 134
The File System Watcher 135
File Read and Write 136
Directory Access and Maintenance 138
File Streams 138
Resource Access with Path 141
Creating a Command-Line Utility 142
Compression/Decompression with ZLib 144
Pipes and ReadLine 148
7 Networking, Sockets, and Security 151
Servers, Streams, and Sockets 151
Sockets and Streams 151
TCP Sockets and Servers 152
UDP/Datagram Socket 157
Guards at the Gate 159
Setting Up TLS/SSL 159
Working with HTTPS 161
The Crypto Module 163
8 Child Processes 169
Child_process.spawn 169
Child_process.exec and child_process.execFile 173
Child_process.fork 175
Running a Child Process Application in Windows 177
9 Node and ES6 179
Strict Mode 179
Let and const 181
Arrow Functions 183
Classes 185
Promises with Bluebird 187
10 Full-Stack Node Development 191
The Express Application Framework 192
MongoDB and Redis Database Systems 198
MongoDB 199
Redis Key/Value Store 202
AngularJS and Other Full-Stack Frameworks 210
11 Node in Development and Production 217
Debugging Node Applications 217
The Node Debugger 217
Node Inspector 222
Unit Testing 226
Unit Testing with Assert 226
Unit Testing with Nodeunit 229
Other Testing Frameworks 231
Keeping Node Up and Running 234
Benchmark and Load Testing with Apache Bench 237
12 Node in New Environments 241
Samsung IoT and GPIO 241
Windows with Chakra Node 243
Node for Microcontrollers and Microcomputers 245
Fritzing 246
Node and Adruino 252
Node and Raspberry Pi 2 259
Index 263