Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Praise for the First Edition

Preface

Acknowledgments

About this Book

About the Author

About the Cover Illustration

1. Welcome to Node

Chapter 1. Welcome to Node.js

1.1. A typical Node web application

1.1.1. Nonblocking I/O

1.1.2. The event loop

1.2. ES2015, Node, and V8

1.2.1. Node and V8

1.2.2. Working with feature groups

1.2.3. Understanding Node’s release schedule

1.3. Installing Node

1.4. Node’s built-in tools

1.4.1. npm

1.4.2. The core modules

1.4.3. The debugger

1.5. The three main types of Node program

1.5.1. Web applications

1.5.2. Command-line tools and daemons

1.5.3. Desktop applications

1.5.4. Applications suited to Node

1.6. Summary

Chapter 2. Node programming fundamentals

2.1. Organizing and reusing Node functionality

2.2. Starting a new Node project

2.2.1. Creating modules

2.3. Fine-tuning module creation by using module.exports

2.4. Reusing modules by using the node_modules folder

2.5. Exploring caveats

2.6. Using asynchronous programming techniques

2.7. Handling one-off events with callbacks

2.8. Handling repeating events with event emitters

2.8.1. An example event emitter

2.8.2. Responding to an event that should occur only once

2.8.3. Creating event emitters: a publish/subscribe example

2.8.4. Extending the event emitter: a file watcher example

2.9. Challenges with asynchronous development

2.10. Sequencing asynchronous logic

2.11. When to use serial flow control

2.12. Implementing serial flow control

2.13. Implementing parallel flow control

2.14. Using community tools

2.15. Summary

Chapter 3. What is a Node web application?

3.1. Understanding a Node web application’s structure

3.1.1. Starting a new web app

3.1.2. Comparing other platforms

3.1.3. What’s next?

3.2. Building a RESTful web service

3.3. Adding a database

3.3.1. Making your own model API

3.3.2. Making articles readable and saving them for later

3.4. Adding a user interface

3.4.1. Supporting multiple formats

3.4.2. Rendering templates

3.4.3. Using npm for client-side dependencies

3.5. Summary

2. Web development with Node

Chapter 4. Front-end build systems

4.1. Understanding front-end development with Node

4.2. Using npm to run scripts

4.2.1. Creating custom npm scripts

4.2.2. Configuring front-end build tools

4.3. Providing automation with Gulp

4.3.1. Adding Gulp to a project

4.3.2. Creating and running Gulp tasks

4.3.3. Watching for changes

4.3.4. Using separate files for larger projects

4.4. Building web apps with webpack

4.4.1. Using bundles and plugins

4.4.2. Configuring and running webpack

4.4.3. Using webpack development server

4.4.4. Loading CommonJS modules and assets

4.5. Summary

Chapter 5. Server-side frameworks

5.1. Personas

5.1.1. Phil: agency developer

5.1.2. Nadine: open source developer

5.1.3. Alice: product developer

5.2. What is a framework?

5.3. Koa

5.3.1. Setting up

5.3.2. Defining routes

5.3.3. REST APIs

5.3.4. Strengths

5.3.5. Weaknesses

5.4. Kraken

5.4.1. Setting up

5.4.2. Defining routes

5.4.3. REST APIs

5.4.4. Strengths

5.4.5. Weaknesses

5.5. hapi

5.5.1. Setting up

5.5.2. Defining routes

5.5.3. Plugins

5.5.4. REST APIs

5.5.5. Strengths

5.5.6. Weaknesses

5.6. Sails.js

5.6.1. Setting up

5.6.2. Defining routes

5.6.3. REST APIs

5.6.4. Strengths

5.6.5. Weaknesses

5.7. DerbyJS

5.7.1. Setting up

5.7.2. Defining routes

5.7.3. REST APIs

5.7.4. Strengths

5.7.5. Weaknesses

5.8. Flatiron.js

5.8.1. Setting up

5.8.2. Defining routes

5.8.3. REST APIs

5.8.4. Strengths

5.8.5. Weaknesses

5.9. LoopBack

5.9.1. Setting up

5.9.2. Defining routes

5.9.3. REST APIs

5.9.4. Strengths

5.9.5. Weaknesses

5.10. Comparison

5.10.1. HTTP servers and routes

5.11. Writing modular code

5.12. Persona choices

5.13. Summary

Chapter 6. Connect and Express in depth

6.1. Connect

6.1.1. Setting up a Connect application

6.1.2. Understanding how Connect middleware works

6.1.3. Combining middleware

6.1.4. Ordering middleware

6.1.5. Creating configurable middleware

6.1.6. Using error-handling middleware

6.2. Express

6.2.1. Generating the application skeleton

6.2.2. Configuring Express and your application

6.2.3. Rendering views

6.2.4. Express routing 101

6.2.5. Authenticating users

6.2.6. Registering new users

6.2.7. Logging in registered users

6.2.8. Working with user-loading middleware

6.2.9. Creating a public REST API

6.2.10. Enabling content negotiation

6.3. Summary

Chapter 7. Web application templating

7.1. Using templating to keep code clean

7.1.1. Templating in action

7.1.2. Rendering HTML without a template

7.2. Templating with Embedded JavaScript

7.2.1. Creating a template

7.2.2. Integrating EJS into your application

7.2.3. Using EJS for client-side applications

7.3. Using the Mustache templating language with Hogan

7.3.1. Creating a template

7.3.2. Using Mustache tags

7.3.3. Fine-tuning Hogan

7.4. Templating with Pug

7.4.1. Pug basics

7.4.2. Logic in Pug templates

7.4.3. Organizing Pug templates

7.5. Summary

Chapter 8. Storing application data

8.1. Relational databases

8.2. PostgreSQL

8.2.1. Performing installation and setup

8.2.2. Creating the database

8.2.3. Connecting to Postgres from Node

8.2.4. Defining tables

8.2.5. Inserting data

8.2.6. Updating data

8.2.7. Querying data

8.3. Knex

8.3.1. jQuery for databases

8.3.2. Connecting and running queries with Knex

8.3.3. Swapping the database back end

8.3.4. Beware of leaky abstractions

8.4. MySQL vs. PostgreSQL

8.5. ACID guarantees

8.5.1. Atomicity: transactions either succeed or fail in entirety

8.5.2. Consistency: constraints are always enforced

8.5.3. Isolation: concurrent transactions don’t interfere

8.5.4. Durability: transactions are permanent

8.6. NoSQL

8.7. Distributed databases

8.8. MongoDB

8.8.1. Performing installation and setup

8.8.2. Connecting to MongoDB

8.8.3. Inserting documents

8.8.4. Querying

8.8.5. Using MongoDB identifiers

8.8.6. Using replica sets

8.8.7. Understanding write concerns

8.9. Key/value stores

8.10. Redis

8.10.1. Performing installation and setup

8.10.2. Performing initialization

8.10.3. Working with key/value pairs

8.10.4. Working with keys

8.10.5. Encoding and data types

8.10.6. Using hashes

8.10.7. Using lists

8.10.8. Using sets

8.10.9. Providing pub/sub with channels

8.10.10. Improving Redis performance

8.11. Embedded databases

8.12. LevelDB

8.12.1. LevelUP and LevelDOWN

8.12.2. Installation

8.12.3. API overview

8.12.4. Initialization

8.12.5. Key/value encodings

8.12.6. Reading and writing key/value pairs

8.12.7. Pluggable back ends

8.12.8. The modular database

8.13. Serialization and deserialization are expensive

8.14. In-browser storage

8.14.1. Web storage: localStorage and sessionStorage

8.14.2. Reading and writing values

8.14.3. localForage

8.14.4. Reading and writing

8.15. Hosted storage

8.15.1. Simple Storage Service

8.16. Which database?

8.17. Summary

Chapter 9. Testing Node applications

9.1. Unit testing

9.1.1. The assert module

9.1.2. Mocha

9.1.3. Vows

9.1.4. Chai

9.1.5. Should.js

9.1.6. Spies and stubs with Sinon.JS

9.2. Functional testing

9.2.1. Selenium

9.3. Dealing with failing tests

9.3.1. Getting more-detailed logs

9.3.2. Getting better stack traces

9.4. Summary

Chapter 10. Deploying Node applications and maintaining uptime

10.1. Hosting Node applications

10.1.1. Platform as a service

10.1.2. Servers

10.1.3. Containers

10.2. Understanding deployment basics

10.2.1. Deploying from a Git repository

10.2.2. Keeping Node running

10.3. Maximizing uptime and performance

10.3.1. Maintaining uptime with Upstart

10.3.2. The cluster API: taking advantage of multiple cores

10.3.3. Hosting static files and proxying

10.4. Summary

3. Beyond web development

Chapter 11. Writing command-line applications

11.1. Understanding conventions and philosophy

11.2. Introducing parse-json

11.3. Using command-line arguments

11.3.1. Parsing command-line arguments

11.3.2. Validating arguments

11.3.3. Passing stdin as a file

11.4. Sharing command-line tools with npm

11.5. Connecting scripts with pipes

11.5.1. Piping data into parse-json

11.5.2. Working with errors and exit codes

11.5.3. Using pipes in Node

11.5.4. Pipes and command execution order

11.6. Interpreting real-world scripts

11.7. Summary

Chapter 12. Conquering the desktop with Electron

12.1. Introducing Electron

12.1.1. Electron’s stack

12.1.2. Interface design

12.2. Creating an Electron app

12.3. Building a full desktop application

12.3.1. Bootstrapping React and Babel

12.3.2. Installing the dependencies

12.3.3. Setting up webpack

12.4. The React app

12.4.1. Defining the Request component

12.4.2. Defining the Response component

12.4.3. Communicating between React components

12.5. Builds and distribution

12.5.1. Building with Electron Packager

12.5.2. Packaging

12.6. Summary

Appendix A. Installing Node

A.1. Installing Node by using an installer

A.1.1. The macOS installer

A.1.2. The Windows installer

A.2. Using other ways to install Node

A.2.1. Installing Node from source

A.2.2. Installing Node with a package manager

Appendix B. Automating the web with scraping

B.1. Understanding web scraping

B.1.1. Uses of web scraping

B.1.2. Required tools

B.2. Performing basic web scraping with cheerio

B.3. Handling dynamic content with jsdom

B.4. Making sense of raw data

B.5. Summary

Appendix C. Connect’s officially supported middleware

C.1. Parsing cookies, request bodies, and query strings

C.1.1. cookie-parser: parse HTTP cookies

C.1.2. Parsing query strings

C.1.3. body-parser: parse request bodies

C.1.4. compression: compressing outgoing responses

C.2. Implementing core web application functions

C.2.1. morgan: log requests

C.2.2. serve-favicon: address bar and bookmark icons

C.2.3. method-override: fake HTTP methods

C.2.4. vhost: virtual hosting

C.2.5. express-session: session management

C.3. Handling web application security

C.3.1. basic-auth: HTTP Basic authentication

C.3.2. csurf: cross-site request forgery protection

C.3.3. errorhandler: displaying errors during development

C.4. Serving static files

C.4.1. serve-static: automatically serving files to the browser

C.4.2. serve-index: generating directory listings

 Glossary

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Appendix A

Index

List of Figures

List of Tables

List of Listings