Node.js in Practice
Alex Young and Marc Harter
Foreword By Ben Noordhuis
  • December 2014
  • ISBN 9781617290930
  • 424 pages
  • printed in black & white

An in-depth tour of Node.js

From the Foreword by Ben Noordhuis, Cofounder of StrongLoop, Inc.

Node.js in Practice is a collection of fully tested examples that offer solutions to the common and not-so-common issues you face when you roll out Node. You'll dig into important topics like the ins and outs of event-based programming, how and why to use closures, how to structure applications to take advantage of end-to-end JavaScript apps, and more.

Table of Contents show full




about this book

about the cover illustration

Part 1 Node fundamentals

1. Getting started

1.1. Getting to know Node

1.1.1. Why Node?

1.1.2. Node’s main features

1.2. Building a Node application

1.2.1. Creating a new Node project

1.2.2. Making a stream class

1.2.3. Using a stream

1.2.4. Writing a test

1.3. Summary

2. Globals: Node’s environment

2.1. Modules

Technique 1 Installing and loading modules

Technique 2 Creating and managing modules

Technique 4 Working with paths

2.2. Standard I/O and the console object

Technique 5 Reading and writing to standard I/O

Technique 6 Logging messages

Technique 7 Benchmarking a program

2.3. Operating system and command-line integration

Technique 8 Getting platform information

Technique 9 Passing command-line arguments

Technique 10 Exiting a program

Technique 11 Responding to signals

Delaying execution with timers

Technique 12 Executing functions after a delay with setTimeout

Technique 13 Running callbacks periodically with timers

Technique 14 Safely managing asynchronous APIs

2.4. Summary

3. Buffers: Working with bits, bytes, and encodings

3.1. Changing data encodings

Technique 15 Converting buffers into other formats

Technique 16 Changing string encodings using buffers

3.2. Converting binary files to JSON

Technique 17 Using buffers to convert raw data

3.3. Creating your own binary protocol

Technique 18 Creating your own network protocol

3.4. Summary

4. Events: Mastering EventEmitter and beyond

4.1. Basic usage

Technique 19 Inheriting from EventEmitter

Technique 20 Mixing in EventEmitter

4.2. Error handling

Technique 21 Managing errors

Technique 22 Managing errors with domains

4.3. Advanced patterns

Technique 23 Reflection

Technique 24 Detecting and exploiting EventEmitter

Technique 25 Categorizing event names

4.4. Third-party modules and extensions

Technique 26 Alternatives to EventEmitter

4.5. Summary

5. Streams: Node’s most powerful and misunderstood feature

5.1. Introduction to streams

5.1.1. Types of streams

5.1.2. When to use streams

5.1.3. History

5.1.4. Streams in third-party modules

5.1.5. Streams inherit from EventEmitter

5.2. Built-in streams

Technique 27 Using built-in streams to make a static web server

Technique 28 Stream error handling

5.3. Third-party modules and streams

Technique 29 Using streams from third-party modules

5.4. Using the stream base classes

Technique 30 Correctly inheriting from the stream base classes

Technique 31 Implementing a readable stream

Technique 32 Implementing a writable stream

Technique 33 Transmitting and receiving data with duplex streams

Technique 34 Parsing data with transform streams

5.5. Advanced patterns and optimization

Technique 35 Optimizing streams

Technique 36 Using the old streams API

Technique 37 Adapting streams based on their destination

Technique 38 Testing streams

5.6. Summary

6. File system: Synchronous and asynchronous approaches to files

6.1. An overview of the fs module

6.1.1. POSIX file I/O wrappers

6.1.2. Streaming

6.1.3. Bulk file I/O

6.1.4. File watching

6.1.5. Synchronous alternatives

Technique 39 Loading configuration files

Technique 40 Using file descriptors

Technique 41 Working with file locking

Technique 42 Recursive file operations

Technique 43 Writing a file database

Technique 44 Watching files and directories

6.2. Summary

7. Networking: Node’s true "Hello, World"

7.1. Networking in Node

7.1.1. Networking terminology

7.1.2. Node’s networking modules

7.1.3. Non-blocking networking and thread pools

7.2. TCP clients and servers

Technique 45 Creating a TCP server and tracking clients

Technique 46 Testing TCP servers with clients

Technique 47 Improve low-latency applications

7.3. UDP clients and servers

Technique 48 Transferring a file with UDP

Technique 49 UDP client server applications

7.4. HTTP clients and servers

Technique 50 HTTP servers

Technique 51 Following redirects

Technique 52 HTTP proxies

7.5. Making DNS requests

Technique 53 Making a DNS request

7.6. Encryption

Technique 54 A TCP server that uses encryption

Technique 55 Encrypted web servers and clients

7.7. Summary

8. Child processes: Integrating external applications with Node

8.1. Executing external applications

Technique 56 Executing external applications

8.1.1. Paths and the PATH environment variable

8.1.2. Errors when executing external applications

Technique 57 Streaming and external applications

8.1.3. Stringing external applications together

Technique 58 Executing commands in a

8.1.4. Security and shell command execution

Technique 59 Detaching a child process

8.1.5. Handing I/O between the child and parent processes

8.1.6. Reference counting and child processes

8.2. Executing Node programs

Technique 60 Executing Node programs

Technique 61 Forking Node modules

Technique 62 Running jobs

8.2.1. Job pooling

8.2.2. Using the pooler module

8.3. Working synchronously

Technique 63 Synchronous child processes

8.4. Summary

Part 2 Real-world recipes

9. The Web: Build leaner and meaner web applications

9.1. Front-end techniques

Technique 64 Quick servers for static sites

Technique 65 Using the DOM in Node

Technique 66 Using Node modules in the browser

9.2. Server-side techniques

Technique 67 Express route separation

Technique 68 Automatically restarting the server

Technique 69 Configuring web applications

Technique 70 Elegant error handling

Technique 71 RESTful web applications

Technique 72 Using custom middleware

Technique 73 Using events to decouple functionality

Technique 74 Using sessions with WebSockets

Technique 75 Migrating Express 3 applications to Express 4

9.3. Testing web applications

Technique 76 Testing authenticated routes

Technique 77 Creating seams for middleware injection

Technique 78 Testing applications that depend on remote services

9.4. Full stack frameworks

9.5. Real-time services

9.6. Summary

10. Tests: The key to confident code

10.1. Introduction to testing with Node

10.2. Writing simple tests with assertions

Technique 79 Writing tests with built-in modules

Technique 80 Testing for errors

Technique 81 Creating custom assertions

10.3. Test harnesses

Technique 82 Organizing tests with a test harness

10.4. Test frameworks

Technique 83 Writing tests with Mocha

Technique 84 Testing web applications with Mocha

Technique 85 The Test Anything Protocol

10.5. Tools for tests

Technique 86 Continuous integration

Technique 87 Database fixtures

10.6. Further reading

10.7. Summary

11. Debugging: Designing for introspection and resolving issues

11.1. Designing for introspection

11.1.1. Explicit exceptions

11.1.2. Implicit exceptions

11.1.3. The error event

11.1.4. The error argument

Technique 88 Handling uncaught exceptions

Technique 89 Linting Node applications

11.2. Debugging issues

Technique 90 Using Node’s built-in debugger

Technique 91 Using Node Inspector

Technique 92 Profiling Node applications

Technique 93 Debugging memory leaks

Technique 94 Inspecting a running program with a REPL

Technique 95 Tracing system calls

11.3. Summary

12. Node in production: Deploying applications safely

12.1. Deployment

Technique 96 Deploying Node applications to the cloud

Technique 97 Using Node with Apache and nginx

Technique 98 Safely running Node on port 80

Technique 99 Keeping Node processes running

Technique 100 Using WebSockets in production

12.2. Caching and scaling

Technique 101 HTTP caching

Technique 102 Using a Node proxy for routing and scaling

Technique 103 Scaling and resiliency with cluster

12.3. Maintenance

Technique 104 Package optimization

Technique 105 Logging and logging services

12.4. Further notes on scaling and resiliency

12.5. Summary

Part 3 Writing modules

13. Writing modules: Mastering what Node is all about

13.1. Brainstorming

13.1.1. A faster Fibonacci module

Technique 106 Planning for our module

Technique 107 Proving our module idea

13.2. Building out the package.json file

Technique 108 Setting up a package.json file

Technique 109 Working with dependencies

Technique 110 Semantic versioning

13.3. The end user experience

Technique 111 Adding executable scripts

Technique 112 Trying out a module

Technique 113 Testing across multiple Node versions

13.4. Publishing

Technique 114 Publishing modules

Technique 115 Keeping modules private

13.5. Summary

Appendix A: Community

A.1. Asking questions

A.2. Hanging out

A.3. Reading

A.4. Training by the community, for the community

A.5. Marketing your open source projects


About the book

You've decided to use Node.js for your next project and you need the skills to implement Node in production. It would be great to have Node experts Alex Young and Marc Harter at your side to help you tackle those day-to-day challenges. With this book, you can!

Node.js in Practice is a collection of 115 thoroughly tested examples and instantly useful techniques guaranteed to make any Node application go more smoothly. Following a common-sense Problem/Solution format, these experience-fueled techniques cover important topics like event-based programming, streams, integrating external applications, and deployment. The abundantly annotated code makes the examples easy to follow, and techniques are organized into logical clusters, so it's a snap to find what you're looking for.

What's inside

  • Common usage examples, from basic to advanced
  • Designing and writing modules
  • Testing and debugging Node apps
  • Integrating Node into existing systems

About the reader

Written for readers who have a practical knowledge of JavaScript and the basics of Node.js.

About the authors

Alex Young is a seasoned JavaScript developer who blogs regularly at DailyJS. Marc Harter works daily on large-scale projects including high-availability real-time applications, streaming interfaces, and other data-intensive systems.

combo $49.99 pBook + eBook
eBook $39.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

The missing manual for Node.js, packed with real-world examples!

Kevin Baister, 1KB Software Solutions Ltd.

Essential recipes for the server-side JavaScript developer.

Gregor Zurowski, Sotheby’s

Useful techniques and resources that help with problem solving, debugging, and troubleshooting.

Michael Piscatello, MBP Enterprises, LLC