售 价:¥
温馨提示:数字商品不支持退换货,不提供源文件,不支持导出打印
为你推荐
Node.js Design Patterns - Second Edition
Node.js Design Patterns - Second Edition
Credits
About the Authors
Acknowledgments
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Welcome to the Node.js Platform
The Node.js philosophy
Small core
Small modules
Small surface area
Simplicity and pragmatism
Introduction to Node.js 6 and ES2015
The let and const keywords
The arrow function
Class syntax
Enhanced object literals
Map and Set collections
WeakMap and WeakSet collections
Template literals
Other ES2015 features
The reactor pattern
I/O is slow
Blocking I/O
Non-blocking I/O
Event demultiplexing
Introducing the reactor pattern
The non-blocking I/O engine of Node.js-libuv
The recipe for Node.js
Summary
2. Node.js Essential Patterns
The callback pattern
The continuation-passing style
Synchronous continuation-passing style
Asynchronous continuation-passing style
Non-continuation-passing style callbacks
Synchronous or asynchronous?
An unpredictable function
Unleashing Zalgo
Using synchronous APIs
Deferred execution
Node.js callback conventions
Callbacks come last
Error comes first
Propagating errors
Uncaught exceptions
The module system and its patterns
The revealing module pattern
Node.js modules explained
A homemade module loader
Defining a module
Defining globals
module.exports versus exports
The require function is synchronous
The resolving algorithm
The module cache
Circular dependencies
Module definition patterns
Named exports
Exporting a function
Exporting a constructor
Exporting an instance
Modifying other modules or the global scope
The observer pattern
The EventEmitter class
Creating and using EventEmitter
Propagating errors
Making any object observable
Synchronous and asynchronous events
EventEmitter versus callbacks
Combining callbacks and EventEmitter
Summary
3. Asynchronous Control Flow Patterns with Callbacks
The difficulties of asynchronous programming
Creating a simple web spider
The callback hell
Using plain JavaScript
Callback discipline
Applying the callback discipline
Sequential execution
Executing a known set of tasks in sequence
Sequential iteration
Web spider version 2
Sequential crawling of links
The pattern
Parallel execution
Web spider version 3
The pattern
Fixing race conditions with concurrent tasks
Limited parallel execution
Limiting the concurrency
Globally limiting the concurrency
Queues to the rescue
Web spider version 4
The async library
Sequential execution
Sequential execution of a known set of tasks
Sequential iteration
Parallel execution
Limited parallel execution
Summary
4. Asynchronous Control Flow Patterns with ES2015 and Beyond
Promise
What is a promise?
Promises/A+ implementations
Promisifying a Node.js style function
Sequential execution
Sequential iteration
Sequential iteration – the pattern
Parallel execution
Limited parallel execution
Exposing callbacks and promises in public APIs
Generators
The basics of generators
A simple example
Generators as iterators
Passing values back to a generator
Asynchronous control flow with generators
Generator-based control flow using co
Sequential execution
Parallel execution
Limited parallel execution
Producer-consumer pattern
Limiting the download tasks concurrency
Async await using Babel
Installing and running Babel
Comparison
Summary
5. Coding with Streams
Discovering the importance of streams
Buffering versus streaming
Spatial efficiency
Gzipping using a buffered API
Gzipping using streams
Time efficiency
Composability
Getting started with streams
Anatomy of streams
Readable streams
Reading from a stream
The non-flowing mode
Flowing mode
Implementing Readable streams
Writable streams
Writing to a stream
Back-pressure
Implementing Writable streams
Duplex streams
Transform streams
Implementing Transform streams
Connecting streams using pipes
Through and from for working with streams
Asynchronous control flow with streams
Sequential execution
Unordered parallel execution
Implementing an unordered parallel stream
Implementing a URL status monitoring application
Unordered limited parallel execution
Ordered parallel execution
Piping patterns
Combining streams
Implementing a combined stream
Forking streams
Implementing a multiple checksum generator
Merging streams
Creating a tarball from multiple directories
Multiplexing and demultiplexing
Building a remote logger
Client side – multiplexing
Server side – demultiplexing
Running the mux/demux application
Multiplexing and demultiplexing object streams
Summary
6. Design Patterns
Factory
A generic interface for creating objects
A mechanism to enforce encapsulation
Building a simple code profiler
Composable factory functions
In the wild
Revealing constructor
A read-only event emitter
In the wild
Proxy
Techniques for implementing proxies
Object composition
Object augmentation
A comparison of the different techniques
Creating a logging Writable stream
Proxy in the ecosystem – function hooks and AOP
ES2015 Proxy
In the wild
Decorator
Techniques for implementing Decorators
Composition
Object augmentation
Decorating a LevelUP database
Introducing LevelUP and LevelDB
Implementing a LevelUP plugin
In the wild
Adapter
Using LevelUP through the filesystem API
In the wild
Strategy
Multi-format configuration objects
In the wild
State
Implementing a basic fail-safe socket
Template
A configuration manager template
In the wild
Middleware
Middleware in Express
Middleware as a pattern
Creating a middleware framework for ØMQ
The Middleware Manager
A middleware to support JSON messages
Using the ØMQ middleware framework
The server
The client
Middleware using generators in Koa
Command
A flexible pattern
The task pattern
A more complex command
Summary
7. Wiring Modules
Modules and dependencies
The most common dependency in Node.js
Cohesion and coupling
Stateful modules
The Singleton pattern in Node.js
Patterns for wiring modules
Hardcoded dependency
Building an authentication server using hardcoded dependencies
The db module
The authService module
The authController module
The app module
Running the authentication server
Pros and cons of hardcoded dependencies
Dependency Injection
Refactoring the authentication server to use DI
The different types of DI
Pros and cons of DI
Service locator
Refactoring the authentication server to use a service locator
Pros and cons of a service locator
Dependency Injection container
Declaring a set of dependencies to a DI container
Refactoring the authentication server to use a DI container
Pros and cons of a DI container
Wiring plugins
Plugins as packages
Extension points
Plugin-controlled vs application-controlled extension
Implementing a logout plugin
Using hardcoded dependencies
Exposing services using a service locator
Exposing services using DI
Exposing services using a DI container
Summary
8. Universal JavaScript for Web Applications
Sharing code with the browser
Sharing modules
Universal Module Definition
Creating an UMD module
Considerations on the UMD pattern
ES2015 modules
Introducing Webpack
Exploring the magic of Webpack
The advantages of using Webpack
Using ES2015 with Webpack
Fundamentals of cross-platform development
Runtime code branching
Build-time code branching
Module swapping
Design patterns for cross-platform development
Introducing React
First React component
JSX, what?!
Configuring Webpack to transpile JSX
Rendering in the browser
The React Router library
Creating a Universal JavaScript app
Creating reusable components
Server-side rendering
Universal rendering and routing
Universal data retrieval
The API server
Proxying requests for the frontend
Universal API client
Asynchronous React components
The web server
Summary
9. Advanced Asynchronous Recipes
Requiring asynchronously initialized modules
Canonical solutions
Preinitialization queues
Implementing a module that initializes asynchronously
Wrapping the module with preinitialization queues
In the wild
Asynchronous batching and caching
Implementing a server with no caching or batching
Asynchronous request batching
Batching requests in the total sales web server
Asynchronous request caching
Caching requests in the total sales web server
Notes about implementing caching mechanisms
Batching and caching with promises
Running CPU-bound tasks
Solving the subset sum problem
Interleaving with setImmediate
Interleaving the steps of the subset sum algorithm
Considerations on the interleaving pattern
Using multiple processes
Delegating the subset sum task to other processes
Implementing a process pool
Communicating with a child process
Communicating with the parent process
Considerations on the multiprocess pattern
Summary
10. Scalability and Architectural Patterns
An introduction to application scaling
Scaling Node.js applications
The three dimensions of scalability
Cloning and load balancing
The cluster module
Notes on the behavior of the cluster module
Building a simple HTTP server
Scaling with the cluster module
Resiliency and availability with the cluster module
Zero-downtime restart
Dealing with stateful communications
Sharing the state across multiple instances
Sticky load balancing
Scaling with a reverse proxy
Load balancing with Nginx
Using a service registry
Implementing a dynamic load balancer with http-proxy and Consul
Peer-to-peer load balancing
Implementing an HTTP client that can balance requests across multiple servers
Decomposing complex applications
Monolithic architecture
The microservice architecture
An example of microservice architecture
Pros and cons of microservices
Every service is expendable
Reusability across platforms and languages
A way to scale the application
The challenges of microservices
Integration patterns in a microservice architecture
The API proxy
API orchestration
Integration with a message broker
Summary
11. Messaging and Integration Patterns
Fundamentals of a messaging system
One-way and request/reply patterns
Message types
Command Message
Event Message
Document Message
Asynchronous messaging and queues
Peer-to-peer or broker-based messaging
Publish/subscribe pattern
Building a minimalist real-time chat application
Implementing the server side
Implementing the client side
Running and scaling the chat application
Using Redis as a message broker
Peer-to-peer publish/subscribe with ØMQ
Introducing ØMQ
Designing a peer-to-peer architecture for the chat server
Using the ØMQ PUB/SUB sockets
Durable subscribers
Introducing AMQP
Durable subscribers with AMQP and RabbitMQ
Designing a history service for the chat application
Implementing a reliable history service using AMQP
Integrating the chat application with AMQP
Pipelines and task distribution patterns
The ØMQ fanout/fanin pattern
PUSH/PULL sockets
Building a distributed hashsum cracker with ØMQ
Implementing the ventilator
Implementing the worker
Implementing the sink
Running the application
Pipelines and competing consumers in AMQP
Point-to-point communications and competing consumers
Implementing the hashsum cracker using AMQP
Implementing the producer
Implementing the worker
Implementing the result collector
Running the application
Request/reply patterns
Correlation identifier
Implementing a request/reply abstraction using correlation identifiers
Abstracting the request
Abstracting the reply
Trying the full request/reply cycle
Return address
Implementing the return address pattern in AMQP
Implementing the request abstraction
Implementing the reply abstraction
Implementing the requestor and the replier
Summary
买过这本书的人还买过
读了这本书的人还在读
同类图书排行榜