Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Node.js in Practice
Alex Young and Marc Harter
Copyright
Brief Table of Contents
Table of Contents
Foreword
Preface
Acknowledgments
Alex Young
Marc Harter
About this Book
Chapter roadmap
Code conventions and downloads
Author Online forum
About the Cover Illustration
Part 1. Node fundamentals
Chapter 1. Getting started
1.1. Getting to know Node
1.1.1. Why Node?
Figure 1.1. An advertising server built with Node
1.1.2. Node’s main features
Figure 1.2. Node’s key parts in context
EventEmitter: An API for events
stream: The basis for scalable I/O
fs: Working with files
net: Create network clients and servers
Global objects and other modules
1.2. Building a Node application
1.2.1. Creating a new Node project
1.2.2. Making a stream class
Listing 1.1. A writable stream that counts
Streams and events
1.2.3. Using a stream
Listing 1.2. Using the CountStream class
Figure 1.3. The three steps to creating a new Node project
1.2.4. Writing a test
Listing 1.3. Using the CountStream class
Assertions
1.3. Summary
Chapter 2. Globals: Node’s environment
2.1. Modules
Technique 1 Installing and loading modules
Problem
Solution
Listing 2.1. Using npm
Discussion
Technique 2 Creating and managing modules
Problem
Solution
Discussion
Listing 2.2. Exporting modules
Listing 2.3. Exporting multiple objects, methods, and values
Listing 2.4. Loading modules with require
Technique 3 Loading a group of related modules
Problem
Solution
Discussion
Listing 2.5. The group/index.js file
Listing 2.6. The group/one.js file
Listing 2.7. A file loading the group of modules
Figure 2.1. Folders as modules
Listing 2.8. A package.json file for a directory containing a module
Technique 4 Working with paths
Problem
Solution
Discussion
Listing 2.9. Path variables
2.2. Standard I/O and the console object
Technique 5 Reading and writing to standard I/O
Problem
Solution
Discussion
Listing 2.10. Path variables
Figure 2.2. Data flows in a simple program that uses stdio.
Technique 6 Logging messages
Problem
Solution
Discussion
Listing 2.11. Path variables
Table 2.1. Formatting placeholders
Stack traces
Technique 7 Benchmarking a program
Problem
Solution
Discussion
Listing 2.12. Benchmarking a function
2.3. Operating system and command-line integration
Technique 8 Getting platform information
Problem
Solution
Discussion
Listing 2.13. Branching based on architecture
Technique 9 Passing command-line arguments
Problem
Solution
Discussion
Listing 2.14. Manipulating command-line arguments
Technique 10 Exiting a program
Problem
Solution
Discussion
Listing 2.15. Returning meaningful exit status codes
Technique 11 Responding to signals
Problem
Solution
Discussion
Listing 2.16. Adding a listener for a POSIX signal
Figure 2.3. Signals originate from a process, and are handled with an event listener.
2.4. Delaying execution with timers
Technique 12 Executing functions after a delay with setTimeout
Problem
Solution
Discussion
Listing 2.17. Combining setTimeout with Function.prototype.bind
Listing 2.18. Using clearTimeout to prevent scheduled functions
Technique 13 Running callbacks periodically with timers
Problem
Solution
Discussion
Listing 2.19. Using setInterval and setTimeout together
Listing 2.20. Keeping a timer alive until the program cleanly exits
Technique 14 Safely managing asynchronous APIs
Problem
Solution
Discussion
Listing 2.21. Incorrectly triggering asynchronous methods with events
Listing 2.22. Triggering events inside process.nextTick
Listing 2.23. Creating the illusion of an always asynchronous API
Visualizing the event loop: setImmediate and process.maxTickDepth
Figure 2.4. Scheduling nextTick on the event loop
2.5. Summary
Chapter 3. Buffers: Working with bits, bytes, and encodings
3.1. Changing data encodings
Technique 15 Converting buffers into other formats
Problem
Solution
Discussion
Technique 16 Changing string encodings using buffers
Problem
Solution
Discussion
Example 1: Creating a Basic authentication header
Example 2: Working with data URIs
Figure 3.1. Data URI read in a browser displays the monkey as an image
Figure 3.2. Generated secondmonkey.png file from a data URI
3.2. Converting binary files to JSON
Technique 17 Using buffers to convert raw data
Problem
Solution
Discussion
Figure 3.3. The transformation of binary data into a more usable/programmable format
Why are we covering in depth a binary format that I may never use?
Figure 3.4. Binary data is read using FileSystem API into Node.js, transformed using the Buffer API into an easier-to-use JSON format.
Figure 3.5. Final result of our transformation
The header
Table 3.1. DBase 5.0 header specification
Note
Figure 3.6. The header: transformation from the specification to code using the Node Buffer API
The field descriptor array
Table 3.2. DBase 5.0 field descriptor array specification
Table 3.3. Field types specification
Figure 3.7. The field descriptor array: transformation from the specification to code using the Node Buffer API
The records
Figure 3.8. The records: transformation from the specification to code using the Node Buffer API
Figure 3.9. The full set of code for parsing a DBF file into JSON
3.3. Creating your own binary protocol
Technique 18 Creating your own network protocol
Problem
Solution
Discussion
Table 3.4. Simple key-value database protocol
Playing with bits to select databases
Zero-indexed arrays
Looking up the key to store the data
Inflating data with zlib
3.4. Summary
Chapter 4. Events: Mastering EventEmitter and beyond
4.1. Basic usage
Technique 19 Inheriting from EventEmitter
Problem
Solution
Listing 4.1. Inheriting from EventEmitter
Discussion
Listing 4.2. Inheriting from EventEmitter
Listing 4.3. Adding multiple listeners
Listing 4.4. Removing listeners
Technique 20 Mixing in EventEmitter
Problem
Solution
Discussion
Listing 4.5. Mixing in EventEmitter
Listing 4.6. utils.merge from Connect
4.2. Error handling
Technique 21 Managing errors
Problem
Solution
Discussion
Listing 4.7. Event-based errors
Technique 22 Managing errors with domains
Problem
Solution
Discussion
Listing 4.8. Managing errors with domain
Figure 4.1. Domains help catch errors and handle them with an EventEmitter-style API.
4.3. Advanced patterns
Technique 23 Reflection
Problem
Solution
Discussion
Listing 4.9. Keeping tabs on new listeners
Listing 4.10. Automatically triggering events based on new listeners
Listing 4.11. Querying listeners
Technique 24 Detecting and exploiting EventEmitter
Problem
Solution
Discussion
Listing 4.12. Reusing EventEmitter in Express
Listing 4.13. Reusing EventEmitter in the redis module
Technique 25 Categorizing event names
Problem
Solution
Discussion
Listing 4.14. Categorizing event names using an object
4.4. Third-party modules and extensions
Technique 26 Alternatives to EventEmitter
Problem
Solution
Discussion
Listing 4.15. Using RabbitMQ with Node
Listing 4.16. Using ØMQ with Node
Listing 4.17. Using Redis Pub/Sub with Node
Listing 4.18. Using Redis Pub/Sub with Node
4.5. Summary
Chapter 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
Figure 5.1. Using streamable APIs means I/O operations potentially use less memory.
5.1.3. History
Streams old and new
Table 5.1. A summary of the classes available in streams2
5.1.4. Streams in third-party modules
Figure 5.2. The baudio module by James Halliday (substack) supports the generation of audio streams (from https://github.com/substack/baudio).
5.1.5. Streams inherit from EventEmitter
Table 5.2. Selecting a streams base class
5.2. Built-in streams
Technique 27 Using built-in streams to make a static web server
Problem
Solution
Discussion
Listing 5.1. A simple static web server that uses streams
Listing 5.2. A static web server with gzip
Figure 5.3. The network inspector confirms the content was compressed.
Technique 28 Stream error handling
Problem
Solution
Discussion
Listing 5.3. Catching errors during streaming
5.3. Third-party modules and streams
Technique 29 Using streams from third-party modules
Problem
Solution
Discussion
Using streams with Express
Listing 5.4. An Express application that uses streams
Using streams with Mongoose
Using streams with MySQL
Summary
5.4. Using the stream base classes
Technique 30 Correctly inheriting from the stream base classes
Problem
Solution
Discussion
Inheriting from the base classes
Listing 5.5. Inheriting from the stream.Readable base class
Summary
Technique 31 Implementing a readable stream
Problem
Solution
Discussion
Listing 5.6. A JSON line parser
Listing 5.7. A stream configured to use objectMode
Technique 32 Implementing a writable stream
Problem
Solution
Discussion
Listing 5.8. An example implementation of a writable stream
Technique 33 Transmitting and receiving data with duplex streams
Problem
Solution
Discussion
Listing 5.9. A duplex stream
Figure 5.4. A duplex stream
Technique 34 Parsing data with transform streams
Problem
Solution
Discussion
Listing 5.10. A CSV parser implemented using a transform stream
5.5. Advanced patterns and optimization
Technique 35 Optimizing streams
Problem
Solution
Discussion
Listing 5.11. Benchmarking streams
Figure 5.5. A graphical representation of the memory usage of streams
Technique 36 Using the old streams API
Problem
Solution
Discussion
Listing 5.12. An old-style stream that has been wrapped
Technique 37 Adapting streams based on their destination
Problem
Solution
Discussion
Listing 5.13. Using isTTY to adapt stream behavior
Technique 38 Testing streams
Problem
Solution
Discussion
Listing 5.14. The CSVParser stream
Listing 5.15. Testing the CSVParser stream
5.6. Summary
Chapter 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
Table 6.1. Supported POSIX file methods in Node
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
Problem
Solution
Discussion
Technique 40 Using file descriptors
Problem
Solution
Discussion
Table 6.2. Common file descriptors
Synchronous logging
Technique 41 Working with file locking
Problem
Solution
Discussion
File Locking with Third-Party Modules
Figure 6.1. Advisory locking using a lockfile between cooperating processes
Creating lockfiles using the exclusive flag
Flag combinations when opening files
Creating lockfiles with mkdir
Making a lockfile module
Technique 42 Recursive file operations
Problem
Solution
Discussion
Alternatives to counters
Third-party solutions to parallel operations
Technique 43 Writing a file database
Problem
Solution
Discussion
Structuring our writes to structure our reads
Technique 44 Watching files and directories
Problem
Solution
Discussion
The story about fs.watch
The story about fs.watchFile
Which one is right for me?
fs.watchFile and directories
6.2. Summary
Chapter 7. Networking: Node’s true “Hello, World”
7.1. Networking in Node
7.1.1. Networking terminology
Table 7.1. Networking concepts
Layers
Figure 7.1. Protocols are grouped into seven logical layers. Packets are wrapped by protocols at consecutive layers.
Figure 7.2. Network layer wrapping
TCP/IP
UDP and how it compares to TCP
Sockets
The Berkeley Sockets API
7.1.2. Node’s networking modules
DNS
HTTP
Encryption
7.1.3. Non-blocking networking and thread pools
Figure 7.3. Node’s threads when making HTTP requests
7.2. TCP clients and servers
Technique 45 Creating a TCP server and tracking clients
Problem
Solution
Discussion
Listing 7.1. A simple TCP server
Technique 46 Testing TCP servers with clients
Problem
Solution
Discussion
Listing 7.2. Creating TCP clients to test servers
Technique 47 Improve low-latency applications
Problem
Solution
Discussion
Figure 7.4. When Nagle’s algorithm is used, smaller packets are collected into a larger payload.
Listing 7.3. Turning off Nagle’s algorithm
7.3. UDP clients and servers
Technique 48 Transferring a file with UDP
Problem
Solution
Discussion
Listing 7.4. A UDP client and server
Technique 49 UDP client server applications
Problem
Solution
Discussion
Figure 7.5. Even though UDP isn’t full-duplex, it’s possible to create connections in two directions given a port number at both sides.
Listing 7.5. Sending messages back to clients
7.4. HTTP clients and servers
Technique 50 HTTP servers
Problem
Solution
Discussion
Listing 7.6. A simple HTTP server
Technique 51 Following redirects
Problem
Solution
Discussion
Figure 7.6. Redirection is cyclical, and requests will be made until a 200 status is encountered.
Listing 7.7. Making an HTTP GET request that follows redirects
Technique 52 HTTP proxies
Problem
Solution
Discussion
Listing 7.8. Using the http module to create a proxy
Figure 7.7. To use the Node proxy we’ve created, set localhost:8080 as the Web Proxy Server.
7.5. Making DNS requests
Technique 53 Making a DNS request
Table 7.2. DNS record types
Problem
Solution
Discussion
7.6. Encryption
Technique 54 A TCP server that uses encryption
Problem
Solution
Discussion
Listing 7.9. A TCP server that uses TLS for encryption
Listing 7.10. A TCP client that uses TLS
Technique 55 Encrypted web servers and clients
Problem
Solution
Discussion
Listing 7.11. A basic HTTP server that uses TLS for encryption
Listing 7.12. An example HTTPS client
7.7. Summary
Chapter 8. Child processes: Integrating external applications with Node
Figure 8.1. Choosing the right method
8.1. Executing external applications
Technique 56 Executing external applications
Problem
Solution
Figure 8.2. The execFile method buffers the result and provides a callback interface.
Discussion
8.1.1. Paths and the PATH environment variable
8.1.2. Errors when executing external applications
Figure 8.3. Common child process errors
Technique 57 Streaming and external applications
Problem
Solution
Figure 8.4. The spawn method returns a streaming interface for I/O.
Discussion
API symmetry
8.1.3. Stringing external applications together
Figure 8.5. Stringing external applications together with spawn
Applying what you’ve learned
Technique 58 Executing commands in a shell
Problem
Solution
Figure 8.6. The exec method runs our commands in a subshell.
Discussion
A single command argument
About shells
8.1.4. Security and shell command execution
Technique 59 Detaching a child process
Problem
Solution
Figure 8.7. Detached child process exists independent of the Node process
Discussion
8.1.5. Handing I/O between the child and parent processes
What are file descriptors?
8.1.6. Reference counting and child processes
8.2. Executing Node programs
Technique 60 Executing Node programs
Problem
Solution
Discussion
Executables on Windows
Executables on UNIX
Publishing executable files in npm
Technique 61 Forking Node modules
Problem
Solution
Figure 8.8. The fork command runs a Node module in a separate process and sets up a communications channel.
Discussion
Internals of interprocess communication
Communicating with forked Node modules
Disconnecting from forked Node modules
Technique 62 Running jobs
Problem
Solution
Discussion
8.2.1. Job pooling
Applying what you’ve learned
8.2.2. Using the pooler module
Going further
8.3. Working synchronously
Technique 63 Synchronous child processes
Problem
Solution
Discussion
Error handing with synchronous child process methods
8.4. Summary
Part 2. Real-world recipes
Chapter 9. The Web: Build leaner and meaner web applications
9.1. Front-end techniques
Technique 64 Quick servers for static sites
Problem
Solution
Discussion
Listing 9.1. A quick static web server
Figure 9.1. Glance has built-in pages for errors.
Alternatives to Grunt
Figure 9.2. Projects that use Grunt typically have a package.json and a Gruntfile.js.
Listing 9.2. A Gruntfile for serving static files
Technique 65 Using the DOM in Node
Problem
Solution
Discussion
Listing 9.3. Scraping a web page with cheerio
Technique 66 Using Node modules in the browser
Problem
Solution
Discussion
Listing 9.4. Node modules in the browser
Listing 9.5. Node modules in the browser
9.2. Server-side techniques
Express 3 and 4
Technique 67 Express route separation
Problem
Solution
Discussion
Listing 9.6. A routing module without the rest of the application
Listing 9.7. A refactored app.js file
Technique 68 Automatically restarting the server
Problem
Solution
Discussion
Listing 9.8. Reloading a Node process
Listing 9.9. Nodemon’s configuration file
Technique 69 Configuring web applications
Problem
Solution
Discussion
Listing 9.10. Configuring an Express application
Listing 9.11. A JSON configuration file loader
Listing 9.12. Loading the configuration directory
Listing 9.13. Using nconf to configure an Express application
Listing 9.14. Loading nconf elsewhere in the application
Technique 70 Elegant error handling
Problem
Solution
Discussion
Listing 9.15. Passing errors to middleware
Listing 9.16. Inheriting errors and including status codes
Listing 9.17. Using an error-handling middleware component
Technique 71 RESTful web applications
Problem
Solution
Discussion
Figure 9.3. Making requests to a REST API
Table 9.1. Choosing the correct HTTP verbs
Listing 9.18. A RESTful resource in Express
Table 9.2. Mapping routes to responses
Listing 9.19. RESTful route handlers
Listing 9.20. A restify application
Listing 9.21. Restify routes
Technique 72 Using custom middleware
Problem
Solution
Discussion
Figure 9.4. Requests can pass through several callbacks until the final response is sent.
Listing 9.22. Three types of middleware
Technique 73 Using events to decouple functionality
Problem
Solution
Discussion
Figure 9.5. Applications can be easier to understand if organized according to the SOLID principles.
Listing 9.23. Using events to structure an application
Listing 9.24. Emitting events
Technique 74 Using sessions with WebSockets
Problem
Solution
Discussion
Figure 9.6. A Node web application should support both standard HTTP requests and WebSockets.
Figure 9.7. Accessing sessions from WebSockets
Listing 9.25. An Express application that uses WebSockets
Listing 9.26. The client-side WebSocket implementation
Technique 75 Migrating Express 3 applications to Express 4
Problem
Solution
Discussion
Table 9.3. Migrating Express middleware components
Listing 9.27. Express 4 middleware
Official migration guide
Listing 9.28. Express 4 middleware
9.3. Testing web applications
Technique 76 Testing authenticated routes
Problem
Solution
Discussion
Figure 9.8. You can test authenticated routes by catching cookies.
Listing 9.29. Testing authenticated requests
Technique 77 Creating seams for middleware injection
Problem
Solution
Discussion
Listing 9.30. Taking control of middleware
Listing 9.31. Injecting new behavior during tests
Technique 78 Testing applications that depend on remote services
Problem
Solution
Discussion
Listing 9.32. A small web store that uses PayPal
Listing 9.33. Mocking PayPal’s IPN requests
Listing 9.34. Testing PayPal
9.4. Full stack frameworks
9.5. Real-time services
Figure 9.9. Cubism.js shows time series values in real time.
9.6. Summary
Chapter 10. Tests: The key to confident code
Table 10.1. Node testing concepts
10.1. Introduction to testing with Node
10.2. Writing simple tests with assertions
CommonJS unit testing
Technique 79 Writing tests with built-in modules
Problem
Solution
Discussion
Listing 10.1. The assert module
Listing 10.2. Testing object equality
Technique 80 Testing for errors
Problem
Solution
Discussion
Listing 10.3. Handling errors from asynchronous APIs
Listing 10.4. Ensuring that exceptions are raised
Technique 81 Creating custom assertions
Problem
Solution
Discussion
Listing 10.5. A custom assertion
10.3. Test harnesses
Technique 82 Organizing tests with a test harness
Problem
Solution
Discussion
Listing 10.6. A package.json with a test script
Listing 10.7. An example test file
Listing 10.8. Running tests in a prescribed manner
10.4. Test frameworks
Technique 83 Writing tests with Mocha
Problem
Solution
Discussion
Listing 10.9. A simple Mocha test
Listing 10.10. A sample module to test
Controlling synchronous and asynchronous behavior
Listing 10.11. The Mocha sample project’s JSON file
Technique 84 Testing web applications with Mocha
Problem
Solution
Discussion
Listing 10.12. A Mocha test for a web application
Figure 10.1. Node can run a web server and requests against it to support web application testing.
Listing 10.13. A web application that can square numbers
Listing 10.14. The refactored Mocha test that uses SuperTest
Technique 85 The Test Anything Protocol
Problem
Solution
Discussion
Listing 10.15. Testing with TAP
Figure 10.2. node-tap uses several reusable submodules to orchestrate tests.
10.5. Tools for tests
Technique 86 Continuous integration
Problem
Solution
Discussion
Listing 10.16. A simple test to try with Travis CI
Listing 10.17. A basic package.json file
Listing 10.18. Travis CI configuration
Figure 10.3. Travis CI running tests
Technique 87 Database fixtures
Problem
Solution
Discussion
Database dumps
Listing 10.19. The assert module
Creating test data with your ORM
Listing 10.20. Preparing test data with an ORM
Mocking the database
Listing 10.21. Stubbing a database
Table 10.2. When to use integration tests, mocks, and stubs
10.6. Further reading
10.7. Summary
Chapter 11. Debugging: Designing for introspection and resolving issues
11.1. Designing for introspection
11.1.1. Explicit exceptions
Regaining throw
11.1.2. Implicit exceptions
Catch implicit exceptions early
11.1.3. The error event
11.1.4. The error argument
Technique 88 Handling uncaught exceptions
Problem
Solution
Discussion
Figure 11.1. Leaking resources when using uncaughtException
Using domains for uncaught exceptions
Technique 89 Linting Node applications
Problem
Solution
Discussion
11.2. Debugging issues
Technique 90 Using Node’s built-in debugger
Problem
Solution
Discussion
Helpful multifile debugger commands
Technique 91 Using Node Inspector
Problem
Solution
Discussion
Figure 11.2. Error screen when no debugging agent is found
Figure 11.3. Node inspector connected to the debugger
Figure 11.4. Using the --debug-brk flag
Technique 92 Profiling Node applications
Problem
Solution
Discussion
Technique 93 Debugging memory leaks
Problem
Solution
Discussion
Figure 11.5. Loading a heap snapshot into the Chrome DevTools
Figure 11.6. Loading a second snapshot for comparison
Figure 11.7. Using the comparison view
Figure 11.8. Examining memory allocations between the snapshots
Figure 11.9. Drilling down to the types of data being created in memory
Technique 94 Inspecting a running program with a REPL
Problem
Solution
Discussion
Figure 11.10. Sample Node REPL session
Figure 11.11. Using Netcat against a REPL server
Inspecting a running process
A note about useGlobal
Adding instrumentation
Technique 95 Tracing system calls
Problem
Solution
Discussion
More on man command
Tracing a running process
Listing open files
About other operating system tools
Homework!
11.3. Summary
Chapter 12. Node in production: Deploying applications safely
12.1. Deployment
Technique 96 Deploying Node applications to the cloud
Problem
Solution
Discussion
Figure 12.1. The jitsu command-line client allows you to sign in.
Figure 12.2. The WebOps management interface
Figure 12.3. Signing in with Heroku
Figure 12.4. The Azure CLI tool
Figure 12.5. Azure’s web interface after creating a website
Table 12.1. Setting environmental variables
Technique 97 Using Node with Apache and nginx
Problem
Solution
Discussion
Figure 12.6. A Node program running alongside Apache or nginx
Listing 12.1. Proxying requests to a Node application with Apache
Listing 12.2. Running a program with runit
Listing 12.3. Proxying requests to a Node application with nginx
Technique 98 Safely running Node on port 80
Problem
Solution
Discussion
Technique 99 Keeping Node processes running
Problem
Solution
Discussion
Listing 12.4. Managing a Node program with Upstart
Run levels
Technique 100 Using WebSockets in production
Problem
Solution
Discussion
Listing 12.5. Adding WebSocket support to nginx
Listing 12.6. Using HAProxy with a Node application
Table 12.2. Comparing server options
Listing 12.7. Using HAProxy with WebSockets
12.2. Caching and scaling
Technique 101 HTTP caching
Problem
Solution
Discussion
Figure 12.7. Browsers either use the local cache or make a conditional request, based on the previous request’s headers.
Technique 102 Using a Node proxy for routing and scaling
Problem
Solution
Discussion
Listing 12.8. Redirecting traffic to another port with http-proxy
Listing 12.9. Routing WebSocket connections separately
Listing 12.10. Scaling using multiple instances of a server
Technique 103 Scaling and resiliency with cluster
Problem
Solution
Discussion
Figure 12.8. A Node process running on a single core
Figure 12.9. Take advantage of more cores by running multiple processes.
Listing 12.11. Clustering a Node web application
Listing 12.12. Recovering from untimely worker death
12.3. Maintenance
Technique 104 Package optimization
Problem
Solution
Discussion
Technique 105 Logging and logging services
Problem
Solution
Discussion
Listing 12.13. logrotate configuration
Figure 12.10. Loggly’s dashboard
12.4. Further notes on scaling and resiliency
12.5. Summary
Part 3. Writing modules
Chapter 13. Writing modules: Mastering what Node is all about
Figure 13.1. Node avoids dependency hell
Dependency graphs
Installing global modules
13.1. Brainstorming
13.1.1. A faster Fibonacci module
Technique 106 Planning for our module
Problem
Solution
Discussion
Surveying the landscape
Figure 13.2. npmjs.com package details page
Embrace doing one thing well
Technique 107 Proving our module idea
Problem
Solution
Discussion
Defining an entry point
Testing our implementation
Benchmarking our implementation
13.2. Building out the package.json file
Technique 108 Setting up a package.json file
Problem
Solution
Discussion
Package options
A note about existing modules
Technique 109 Working with dependencies
Problem
Solution
Discussion
Figure 13.3. The different types of dependencies
Main and development dependencies
Optional dependencies
Homework
Peer dependencies
Keeping dependencies up to date
Outdated dependencies that you directly require
Version tags and ranges
Technique 110 Semantic versioning
Figure 13.4. Semantic versioning
Problem
Solution
Discussion
Version operators
Versioning dependencies
Versioning the module
The change log
13.3. The end user experience
Technique 111 Adding executable scripts
Problem
Solution
Discussion
Testing executables with npm link
Technique 112 Trying out a module
Problem
Solution
Discussion
Figure 13.5. Sample output from fastfibserver
Another way to link
Technique 113 Testing across multiple Node versions
Problem
Solution
Discussion
About Node versions
What if my module loses support for a particular Node version?
13.4. Publishing
Technique 114 Publishing modules
Problem
Solution
Discussion
Changing existing account details
Before you publish
Publishing to npm
Undoing a publish
Technique 115 Keeping modules private
Problem
Solution
Discussion
Sharing private modules with Git
Including public repositories
Sharing private modules as a URL
A note about public endpoints
Sharing modules with a private npm registry
13.5. Summary
Appendix. Community
A.1. Asking questions
A.2. Hanging out
A.3. Reading
Figure A.1. James Halliday’s blog about Node and testing
A.4. Training by the community, for the community
Figure A.2. Learn Node with NodeSchool.
A.5. Marketing your open source projects
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
List of Figures
List of Tables
List of Listings
← Prev
Back
Next →
← Prev
Back
Next →