Node Cookbook - Third Edition

Author: David Mark Clements;Matthias Buus;Matteo Collina  

Publisher: Packt Publishing‎

Publication year: 2017

E-ISBN: 9781785881244

P-ISBN(Paperback): 9781785880087

Subject: TP312 程序语言、算法语言

Keyword: 程序语言、算法语言

Language: ENG

Access to resources Favorite

Disclaimer: Any content in publications that violate the sovereignty, the constitution or regulations of the PRC is not accepted or approved by CNPIEC.

Node Cookbook - Third Edition

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 • Security between Node.js and browser applications explained and applied in depth • Cutting edge techniques and tools for measuring and improving performance • Contemporary techniques to create developer-ergonomic, 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 • Rapidly become proficient at debugging Node.js programs • Write and publish your own Node.js modules • Become deeply acquainted with 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 infinite-capacity data processing • Fast-track performance knowledge and optimization abilities • Compare and contrast various persistence strategies, including database integrations with MongoDB, MySQL/MariaDB, Postgres, Redis, and LevelDB • Grasp and apply critically essential security concepts • Understand how to use Node with best-of-breed deployment technologies: Docker, Kubernetes and AWS

Chapter

Preface

Chapter 1: Debugging Processes

Introduction

Debugging Node with Chrome Devtools

Getting ready

How to do it...

How it works...

There's more...

Using node-inspector with older node versions

Immediately pausing a process on start

node debug

See also

Enhancing stack trace output

Getting ready

How to do it...

How it works...

There's more...

Infinite stack trace limit in development

Stack trace layout

Asynchronous stack traces

See also

Enabling debug logs

Getting ready

How to do it...

How it works...

There's more...

Instrumenting code with debug

Using debug in production

JSON logging with pino-debug

See also

Enabling core debug logs

Getting ready

How to do it...

How it works...

There's more...

Creating our own NODE_DEBUG flags

Debugging Node core libraries

See also

Chapter 2: Writing Modules

Introduction

Scaffolding a module

Getting ready

How to do it...

How it works...

There's more...

Reinitializing

Versioning

See also

Installing dependencies

Getting ready

How to do it...

How it works...

There's more...

Installing development dependencies

Using npm run scripts

Eliminating the need for sudo

See also

Writing module code

Getting ready

How to do it...

How it works...

There's more...

Adding tests

Modernizing syntax

See also

Publishing a module

Getting ready

How to do it...

How it works...

There's more...

Detecting Vulnerabilities

Extraneous dependencies

Prepublish

Decentralized publishing

See also

Using a private repository

Getting ready

How to do it...

How it works...

There's more…

Module caching

Scope registries

See also

Chapter 3: Coordinating I/O

Introduction

Interfacing with standard I/O

Getting ready

How to do it...

How it works...

There's more...

Piping

TTY Detection

See also

Working with files

Getting ready

How to do it...

How it works...

There's more...

Asynchronous file operations

Incremental Processing

See also

Fetching metadata

Getting ready

How to do it...

How it works...

There's more...

Getting symlink information

Checking file existence

Manipulating metadata

See also

Watching files and directories

Getting ready

How to do it...

How it works...

There's more...

Watching directories with chokidar

See also

Communicating over sockets

Getting ready

How to do it...

How it works...

There's more...

net sockets are streams

Unix sockets

UDP sockets

See also

Chapter 4: Using Streams

Introduction

Processing Big Data

Getting ready

How to do it...

How it works...

There's more...

Types of stream

Processing infinite amounts of data

Flow mode versus pull-based streaming

Understanding stream events

See also

Using the pipe method

Getting ready

How to do it...

How it works...

There's more...

Keeping piped streams alive

See also

Piping streams in production

Getting ready

How to do it...

How it works...

There's more...

Use pumpify to expose pipelines

See also

Creating transform streams

Getting ready

How to do it...

How it works...

There's more...

Transform streams with Node's core stream module

Creating object mode transform streams

See also

Creating readable and writable streams

Getting ready

How to do it...

How it works...

There's more...

Readable and writable streams with Node's core stream module

Core readable streams flow control issue

Composing duplex streams

See also

Decoupling I/O

Getting ready

How to do it...

How it works...

There's more...

Stream destruction

Handling backpressure

See also

Chapter 5: Wielding Web Protocols

Introduction

Creating an HTTP server

Getting ready

How to do it...

How it works...

There's more...

Binding to a random free port

Dynamic content

See also

Receiving POST data

Getting ready

How to do it...

How it works...

There's more...

Accepting JSON

See also

Handling file uploads

Getting ready

How to do it...

How it works...

There's more...

Processing all field types in multipart data

Uploading files via PUT

See also

Making an HTTP POST request

Getting ready

How to do it...

How it works...

HTTPS requests

There's more...

Buffering a GET request

Streaming payloads

Multipart POST uploads

See also

Communicating with WebSockets

Getting ready

How to do it...

How it works...

There's more...

Creating a Node.js WebSocket client

See also

Creating an SMTP server

Getting ready

How to do it...

How it works...

There's more...

Making an SMTP client

See also

Chapter 6: Persisting to Databases

Introduction

Connecting and sending SQL to a MySQL server

Getting ready

How to do it...

How it works...

There's more...

Avoiding SQL injection

Querying a MySQL database

Connecting and sending SQL to a Postgres server

Getting ready

How to do it...

How it works...

There's more...

Using native bindings

Storing object-modelled data

Storing and retrieving data with MongoDB

Getting ready

How to do it...

How it works...

There's more...

Indexing and aggregation

Updating modifiers, sort, and limit

Storing and retrieving data with Redis

Getting ready

How to do it...

How it works...

There's more...

Command batching

Using Redis

Authenticating

Embedded persistence with LevelDB

Getting ready

How to do it...

How it works...

There's more...

Alternative storage adapters

Chapter 7: Working with Web Frameworks

Introduction

Creating an express web app

Getting ready

How to do it...

How it works...

There's more...

Production

Route parameters and POST requests

Creating middleware

See also

Creating a Hapi web app

Getting ready

How to do it...

How it works...

There's more...

Creating a plugin

Label selecting

See also

Creating a Koa web app

Getting ready

How to do it...

How it works...

There's more...

Creating middleware

Performing asynchronous lookups

See also

Adding a view layer

Getting ready

How to do it...

How it works...

There's more...

Adding a view layer to Koa

Adding a view layer to Hapi

ES2015 template strings as views

See also

Adding logging

Getting ready

How to do it...

How it works...

There's more...

Pino transports and prettifying

Logging with Morgan

Logging with Winston

Adding logging to Koa

Adding logging to Hapi

Capturing debug logs with with Pino

See also

Implementing authentication

Getting ready

How to do it...

How it works...

There's more...

Session authentication in Hapi

Session authentication in Koa

See also

Chapter 8: Dealing with Security

Introduction

Detecting dependency vulnerabilities

Getting ready

How to do it...

How it works...

There's more...

Module vetting

Restricting core module usage

See also

Hardening headers in web frameworks

Getting ready

How to do it...

How it works...

There's more...

Avoiding fingerprinting

Hardening a core HTTP server

Hardening Koa

Hardening Hapi

See also

Anticipating malicious input

Getting ready

How to do it...

How it works...

There's more...

Buffer safety

Dealing with JSON pollution

See also

Guarding against cross site scripting (XSS)

Getting ready

How to do it...

How it works...

There's more...

Preventing protocol-handler-based XSS

Parameter validation

Escaping in JavaScript contexts

See also

Preventing cross site request forgery

Getting ready

How to do it...

How it works...

There's more...

Securing older browsers

See also

Chapter 9: Optimizing Performance

Introduction

Benchmarking HTTP

Getting ready

How to do it...

How it works...

There's more...

Profiling for production

Measuring POST performance

See also

Finding bottlenecks with flamegraphs

Getting ready

How to do it...

How it works...

There's more...

Finding a solution

How 0x works

CPU profiling with Chrome Devtools

See also

Optimizing a synchronous function call

Getting ready

How to do it...

How it works...

There's more...

Function inlining

Checking the optimization status

Tracing optimization and deoptimization events

See also

Optimizing asynchronous callbacks

Getting ready

How to do it...

How it works...

There's more...

A database solution

A caching solution

See also

Profiling memory

Getting ready

How to do it...

How it works...

There's more...

Visualizing memory usage in the Terminal

See also

Chapter 10: Building Microservice Systems

Introduction

Creating a simple RESTful microservice

Getting ready

How to do it...

How it works...

There's more...

Using the core HTTP module

Testing microservices with a browser

See also

Consuming a service

Getting ready

How to do it...

How it works...

There's more...

Integration testing

See also

Setting up a development environment

Getting ready

How to do it...

How it works...

There's more...

A minimal alternative to fuge

Debug command

Shell pass through

Apply command

See also

Standardizing service boilerplate

Getting ready

How to do it...

How it works...

There's more...

Unit testing

Pattern routing

See also

Using containerized infrastructure

Getting ready

How to do it...

How it works...

There's more...

Running containers in the background

See also

Service discovery with DNS

Getting ready

How to do it...

How it works...

There's more...

Alternative service discovery mechanisms

Viewing the environment and DNS Zone

See also

Adding a Queue Based Service

Getting ready

How to do it...

How it works...

Single responsibility

Loose coupling

Vertical separation

Stateless

A note on security

There's more...

Entering a containers shell environment

Saving container state

Cleaning up containers

See also

Chapter 11: Deploying Node.js

Introduction

Building a container for a Node.js process

Getting ready

How to do it...

How it works...

There's more...

Viewing the layers in a Docker image

Adding a new layer

See also

Running a Docker registry

Getting ready

How to do it...

Linux troubleshooting

How it works...

There's more...

Tagging

See also

Storing images on DockerHub

Getting ready

How to do it...

How it works...

There's more...

Using a specific version tag

See also

Deploying a container to Kubernetes

Getting ready

How to do it...

How it works...

There's more...

Using the minikube dashboard

Pushing microservice updates into Kubernetes

See also

Creating a deployment pipeline

Getting ready

How to do it...

How it works...

There's more...

Debugging the build

Automating the build trigger

See also

Deploying a full system

Getting ready

How to do it...

How it works...

There's more...

Running a report

See also

Deploying to the cloud

Getting ready

How to do it...

How it works...

There's more...

Running the dashboard

Inspecting the kops State Store

See also

Index

The users who browse this book also browse