Chapter 1. Welcome to Node.js
Listing 1.1. Using core modules and streams
Chapter 2. Node programming fundamentals
Listing 2.1. Defining a Node module (currency.js)
Listing 2.2. Requiring a module (test_currency.js)
Listing 2.3. Module won’t work as expected
Listing 2.4. A list of post titles
Listing 2.5. A basic HTML template to render the blog titles
Listing 2.6. Using callbacks in a simple application
Listing 2.7. Reducing nesting by creating intermediary functions
Listing 2.8. Reducing nesting by returning early
Listing 2.9. Using the on method to respond to events
Listing 2.10. Using the once method to respond to a single event
Listing 2.11. A simple publish/subscribe system using an event emitter
Listing 2.12. Creating a listener to clean up when clients disconnect
Listing 2.13. Extending the event emitter’s functionality
Listing 2.14. How scope behavior can lead to bugs
Listing 2.15. Using an anonymous function to preserve a global variable’s value
Listing 2.16. Serial control using a community-created add-on
Listing 2.17. Serial flow control implemented in a simple application
Listing 2.18. Parallel flow control implemented in a simple application
Listing 2.19. Using a community add-on flow-control tool in a simple application
Chapter 3. What is a Node web application?
Listing 3.1. RESTful routes example
Listing 3.2. Adding a body parser
Listing 3.4. Adding the Article model to the HTTP routes
Chapter 4. Front-end build systems
Listing 4.1. A gulpfile for ES2015 and React with Babel
Chapter 5. Server-side frameworks
Listing 5.1. Koa’s middleware ordering
Listing 5.2. Basic hapi server
Listing 5.3. hapi hello world server
Chapter 6. Connect and Express in depth
Listing 6.1. Using multiple Connect middleware components
Listing 6.2. Wrong: hello middleware component before logger component
Listing 6.3. A configurable logger middleware component for Connect
Listing 6.4. Error-handling middleware in Connect
Listing 6.5. A minimal Express application
Listing 6.6. Generated Express application skeleton
Listing 6.7. A model for entries
Listing 6.8. Logic to retrieve a range of entries
Listing 6.9. A form for entering post data
Listing 6.10. Add an entry using submitted form data
Listing 6.12. The entries.ejs view
Listing 6.13. Two more potential, but imperfect, attempts at validation middleware
Listing 6.14. Validation middleware implementation
Listing 6.15. Starting to create a user model
Listing 6.16. Updating user records
Listing 6.17. Adding bcrypt encryption to the user model
Listing 6.18. Testing the user model
Listing 6.19. Querying with the Redis command-line tool
Listing 6.20. Fetching a user from Redis
Listing 6.21. Authenticating a user’s name and password
Listing 6.22. Adding registration routes
Listing 6.23. A view template that provides a registration form
Listing 6.24. Creating a user with submitted data
Listing 6.25. A view template for a login form
Listing 6.26. A route to handle logins
Listing 6.27. Anonymous and authenticated user menu template
Listing 6.28. Middleware that loads a logged-in user’s data
Listing 6.29. Enabling user-loading middleware
Listing 6.30. CSS that can be added to style.css to style application menus
Listing 6.32. Pagination middleware
Chapter 7. Web application templating
Listing 7.1. Blog entries text file
Listing 7.2. Blog entry file-parsing logic for a simple blogging application
Listing 7.3. Template engines separate presentation details from application logic
Listing 7.4. An EJS template for displaying blog entries
Listing 7.5. Storing template code in files
Listing 7.6. EJS template that renders an array of students
Listing 7.7. Using EJS to add templating capabilities to the client side
Listing 7.8. Using a lambda in Hogan
Listing 7.9. Using partials in Hogan
Listing 7.10. Template inheritance in action
Listing 7.11. Using block appending to load an additional JavaScript file
Chapter 8. Storing application data
Listing 8.1. Connecting to the database
Listing 8.2. Defining a schema
Listing 8.6. Using Knex to connect and query sqlite3
Listing 8.7. Interacting with the Knex-powered API
Listing 8.8. Connecting to MongoDB
Listing 8.9. Inserting a document
Listing 8.10. Implementing the Article API with MongoDB
Listing 8.11. Starting a replica set
Listing 8.12. Initiating a replica set
Listing 8.13. Connecting to a replica set
Listing 8.14. Connecting to Redis and listening for status events
Listing 8.15. Storing data in elements of Redis hashes
Listing 8.16. A simple chat server implemented with Redis pub/sub functionality
Listing 8.17. Initializing a level database
Listing 8.18. Reading and writing values
Listing 8.19. Getting keys that don’t exist
Listing 8.20. Overriding encoding for specific operations
Listing 8.21. Using memdown with LevelUP
Listing 8.22. Serialization benchmarking
Listing 8.23. Storing JSON in web storage
Listing 8.24. Iterating over entire dataset in localStorage
Listing 8.25. Namespacing keys
Listing 8.26. Getting all items in a namespace
Listing 8.27. Using localStorage for persistent memoization
Listing 8.28. Comparison of getting data with localStorage vs. localForage
Chapter 9. Testing Node applications
Listing 9.1. A model for a to-do list
Listing 9.2. Set up necessary modules
Listing 9.3. Test to make sure that no to-do items remain after deletion
Listing 9.4. Test to make sure adding a to-do works
Listing 9.5. Test whether the doAsync callback is passed true
Listing 9.6. Test whether add throws when missing a parameter
Listing 9.7. Running the tests and reporting test completion
Listing 9.8. Basic structure for a Mocha test
Listing 9.9. Describing the memdb .save functionality
Listing 9.10. Added save functionality
Listing 9.11. Adding a beforeEach hook
Listing 9.12. Testing asynchronous logic
Listing 9.13. Using Vows to test the to-do application
Listing 9.14. Chai’s assert interface
Listing 9.15. Logic for calculating tips when splitting a bill
Listing 9.16. Logic that calculates tips when splitting a bill
Listing 9.20. Sample Express project
Chapter 10. Deploying Node applications and maintaining uptime
Listing 10.1. A typical Upstart configuration file
Listing 10.2. A demonstration of Node’s cluster API
Listing 10.3. A demonstration of Node’s cluster API
Listing 10.4. A configuration file that uses Nginx to proxy Node.js and serve static files
Chapter 11. Writing command-line applications
Chapter 12. Conquering the desktop with Electron
Listing 12.1. A simple Node module
Listing 12.2. Loading Node modules from the rendering process
Listing 12.3. webpack.config.js
Listing 12.5. The Request class
Appendix B. Automating the web with scraping
Listing B.1. Extracting a book’s details
Listing B.2. Dealing with messy HTML
Listing B.3. Scraping with jsdom
Appendix C. Connect’s officially supported middleware
Listing C.1. Reading cookies sent in a request
Listing C.2. Parsing signed cookies
Listing C.3. Parsing query strings
Listing C.4. Parsing form requests
Listing C.5. Validating form requests
Listing C.6. Validating form requests
Listing C.7. Handling uploaded files
Listing C.8. Using the morgan module for logging
Listing C.9. A broken user-update application
Listing C.10. Using method-override to support HTTP PUT
Listing C.11. Using sessions in Connect
Listing C.12. Using Redis as a session store