0%

Book Description

Over 60 high-quality recipes covering debugging, security, performance, microservices, web frameworks, databases, deployment and more; rewritten for Node 4, 6, and 8.

About This Book
  • Actionable recipes across the full spectrum of Node.js development
  • Cutting edge techniques and tools for measuring and improving performance
  • Best practices for creating readily-scalable production systems
Who This Book Is For

If you have good knowledge of JavaScript and want to build fast, efficient, scalable client-server solutions, then this book is for you. Some experience with Node.js is assumed to get the most out of this book. If working from a beginner level Node Cookbook 2nd Edition is recommended as a primer for Node Cookbook 3rd Edition.

What You Will Learn

  • Debug Node.js programs
  • Write and publish your own Node.js modules
  • Detailed coverage of Node.js core API's
  • Use web frameworks such as Express, Hapi and Koa for accelerated web application development
  • Apply Node.js streams for low-footprint data processing
  • Fast-track performance knowledge and optimization abilities
  • Persistence strategies, including database integrations with MongoDB, MySQL/MariaDB, Postgres, Redis, and LevelDB
  • Apply critical, essential security concepts
  • Use Node with best-of-breed deployment technologies: Docker, Kubernetes and AWS
In Detail

Today's web demands efficient real-time applications and scalability. Asynchronous event-driven programming is ideal for this, and this is where Node.js comes in. Server-side JavaScript has been here since the 90s, but Node got it right.

With Node for tooling and server-side logic, and a browser-based client-side UI, everything is JavaScript. This leads to rapid, fluid development cycles. The full-stack, single language experience means less context-switching between languages for developers, architects and whole teams.

This book shows you how to build fast, efficient, and scalable client-server solutions using the latest versions of Node.

The book begins with debugging tips and tricks of the trade, and how to write your own modules. Then you'll learn the fundamentals of streams in Node.js, discover I/O control, and how to implement the different web protocols.

You'll find recipes for integrating databases such as MongoDB, MySQL/MariaDB, Postgres, Redis, and LevelDB. We also cover the options for building web application with Express, Hapi and Koa.

You will then learn about security essentials in Node.js and advanced optimization tools and techniques.

By the end of the book you will have acquired the level of expertise to build production-ready and scalable Node.js systems.

The techniques and skills you will learn in this book are based on the best practices developed by nearForm, one of the leaders in Node implementations, who supported the work of the authors on this book.

Style and approach

This recipe-based practical guide presents each topic with step-by-step instructions on how you can create fast and efficient server side applications using the latest features and capabilities in Node 8 whilst also supporting usage with Node 4 and 6.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Debugging process*
    1. Introduction
    2. Debugging Node with Chrome Devtools
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using node-inspector with older Node versions
        2. Immediately pausing a process on start
        3. node debug
      5. See also
    3. Enhancing stack trace output
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Infinite stack trace limit in development
        2. Stack trace layout
        3. Asynchronous stack traces
      5. See also
    4. Enabling debug logs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Instrumenting code with debug
        2. Using debug in production
        3. JSON logging with pino-debug
      5. See also
    5. Enabling core debug logs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Creating our own NODE_DEBUG flags
        2. Debugging Node core libraries
      5. See also
  3. Writing Modules
    1. Introduction
    2. Scaffolding a module
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Reinitializing
          1. Versioning
      5. See also
    3. Installing dependencies
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Installing development dependencies
        2. Using npm run scripts
        3. Eliminating the need for sudo
      5. See also
    4. Writing module code
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Adding tests
        2. Modernizing syntax
      5. See also
    5. Publishing a module
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Detecting Vulnerabilities
        2. Extraneous dependencies
        3. Prepublish
        4. Decentralized publishing
      5. See also
    6. Using a private repository
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more…
        1. Module caching
        2. Scope registries
      5. See also
  4. Coordinating I/O
    1. Introduction
    2. Interfacing with standard I/O
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Piping
        2. TTY Detection
      5. See also
    3. Working with files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Asynchronous file operations
        2. Incremental Processing
      5. See also
    4. Fetching metadata
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Getting symlink information
        2. Checking file existence
        3. Manipulating metadata
      5. See also
    5. Watching files and directories
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Watching directories with chokidar
      5. See also
    6. Communicating over sockets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. net sockets are streams
        2. Unix sockets
        3. UDP sockets
      5. See also
  5. Using Streams
    1. Introduction
    2. Processing Big Data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Types of stream
        2. Processing infinite amounts of data
        3. Flow mode versus pull-based streaming
        4. Understanding stream events
      5. See also
    3. Using the pipe method
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Keeping piped streams alive
      5. See also
    4. Piping streams in production
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Use pumpify to expose pipelines
      5. See also
    5. Creating transform streams
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Transform streams with Node's core stream module
        2. Creating object mode transform streams
      5. See also
    6. Creating readable and writable streams
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Readable and writable streams with Node's core stream module
        2. Core readable streams flow control issue
        3. Composing duplex streams
      5. See also
    7. Decoupling I/O
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Stream destruction
        2. Handling backpressure
      5. See also
  6. Wielding Web Protocols
    1. Introduction
    2. Creating an HTTP server
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Binding to a random free port
        2. Dynamic content
      5. See also
    3. Receiving POST data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Accepting JSON
      5. See also
    4. Handling file uploads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Processing all field types in multipart data
        2. Uploading files via PUT
      5. See also
    5. Making an HTTP POST request
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. HTTPS requests
      4. There's more...
        1. Buffering a GET request
        2. Streaming payloads
        3. Multipart POST uploads
      5. See also
    6. Communicating with WebSockets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Creating a Node.js WebSocket client
      5. See also
    7. Creating an SMTP server
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Making an SMTP client
      5. See also
  7. Persisting to Databases
    1. Introduction
    2. Connecting and sending SQL to a MySQL server
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Avoiding SQL injection
        2. Querying a MySQL database
      5. See also...
    3. Connecting and sending SQL to a Postgres server
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using native bindings
        2. Storing object-modelled data
      5. See also...
    4. Storing and retrieving data with MongoDB
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Indexing and aggregation
        2. Updating modifiers, sort, and limit
      5. See also
    5. Storing and retrieving data with Redis
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Command batching
        2. Using Redis
        3. Authenticating
      5. See also...
    6. Embedded persistence with LevelDB
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Alternative storage adapters
      5. See also...
  8. Working with Web Frameworks
    1. Introduction
    2. Creating an express web app
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Production
        2. Route parameters and POST requests
        3. Creating middleware
      5. See also
    3. Creating a Hapi web app
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Creating a plugin
        2. Label selecting
      5. See also
    4. Creating a Koa web app
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Creating middleware
        2. Performing asynchronous lookups
      5. See also
    5. Adding a view layer
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Adding a view layer to Koa
        2. Adding a view layer to Hapi
        3. ES2015 template strings as views
      5. See also
    6. Adding logging
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Pino transports and prettifying
        2. Logging with Morgan
        3. Logging with Winston
        4. Adding logging to Koa
        5. Adding logging to Hapi
        6. Capturing debug logs with with Pino
      5. See also
    7. Implementing authentication
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Session authentication in Hapi
        2. Session authentication in Koa
      5. See also
  9. Dealing with Security
    1. Introduction
    2. Detecting dependency vulnerabilities
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Module vetting
        2. Restricting core module usage
      5. See also
    3. Hardening headers in web frameworks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Avoiding fingerprinting
        2. Hardening a core HTTP server
        3. Hardening Koa
        4. Hardening Hapi
      5. See also
    4. Anticipating malicious input
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Buffer safety
        2. Dealing with JSON pollution
      5. See also
    5. Guarding against Cross Site Scripting (XSS)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Preventing protocol-handler-based XSS
        2. Parameter validation
        3. Escaping in JavaScript contexts
      5. See also
    6. Preventing Cross Site Request Forgery
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Securing older browsers
      5. See also
  10. Optimizing Performance
    1. Introduction
    2. Benchmarking HTTP
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Profiling for production
        2. Measuring POST performance
      5. See also
    3. Finding bottlenecks with flamegraphs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Finding a solution
        2. How 0x works
        3. CPU profiling with Chrome Devtools
      5. See also
    4. Optimizing a synchronous function call
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Function inlining
        2. Checking the optimization status
        3. Tracing optimization and deoptimization events
      5. See also
    5. Optimizing asynchronous callbacks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. A database solution
        2. A caching solution
      5. See also
    6. Profiling memory
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Visualizing memory usage in the Terminal
      5. See also
  11. Building Microservice Systems
    1. Introduction
      1. Creating a simple RESTful microservice
      2. Getting ready
      3. How to do it...
      4. How it works...
      5. There's more...
        1. Using Node's core http module
        2. Testing microservices with a browser
      6. See also
    2. Consuming a service
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Integration testing
      5. See also
    3. Setting up a development environment
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. A minimal alternative to fuge
        2. Fuge's debug command
        3. Shell pass through in Fuge
        4. Fuge's  apply command
      5. See also
    4. Standardizing service boilerplate
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Unit testing
        2. Pattern routing
      5. See also
    5. Using containerized infrastructure
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Running containers in the background
      5. See also
    6. Service discovery with DNS
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Alternative service discovery mechanisms
        2. Viewing the environment and DNS Zone
      5. See also
    7. Adding a Queue Based Service
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Single responsibility
        2. Loose coupling
        3. Vertical separation
        4. Stateless
        5. A note on security
      4. There's more...
        1. Entering a containers shell environment
        2. Saving container state
        3. Cleaning up containers
      5. See also
  12. Deploying Node.js
    1. Introduction
    2. Building a container for a Node.js process
      1. Getting ready
      2. There's more...
        1. Viewing the layers in a Docker image
        2. Adding a new layer
        3. Docker alternatives & container standards
    3. Running a Docker registry
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Tagging
      5. See also
    4. Storing images on DockerHub
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using a specific version tag
      5. See also
    5. Deploying a container to Kubernetes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using the minikube dashboard
        2. Pushing microservice updates into Kubernetes
      5. See also
    6. Creating a deployment pipeline
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Debugging the build
        2. Automating the build trigger
      5. See also
    7. Deploying a full system
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Running a report
      5. See also
    8. Deploying to the cloud
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Running the dashboard
        2. Inspecting the kops State Store
      5. See also
18.217.203.172