You are previewing Beginning Node.js.
O'Reilly logo
Beginning Node.js

Book Description

Beginning Node.js is your step-by-step guide to learning all the aspects of creating maintainable Node.js applications. You will see how Node.js is focused on creating high-performing, highly-scalable websites, and how easy it is to get started. Many front-end devs regularly work with HTML, CSS, PHP, even WordPress, but haven't yet got started with Node.js. This book explains everything for you from a beginner level, enabling you to start using Node.js in your projects right away.

Using this book you will learn important Node.js concepts for server-side programming. You will begin with an easy-to-follow pure JavaScript primer, which you can skip if you're confident of your JS skills. You'll then delve into Node.js concepts such as streams and events, and the technology involved in building full-stack Node.js applications. You'll also learn how to test your Node.js code, and deploy your Node.js applications on the internet.

Node.js is a great and simple platform to work with. It is lightweight, easy to deploy and manage. You will see how using Node.js can be a fun and rewarding experience - start today with Beginning Node.js.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. Chapter 1: Setting Up for Node.js Development
    1. Installing Node.js
      1. Installing on Windows
      2. Installing on Mac OS X
    2. Using the REPL
    3. Executing Node.js Scripts
    4. Setting Up an Integrated Development Environment
      1. WebStorm Node.js Support
      2. Visual Studio Node.js Support
    5. Summary
  12. Chapter 2: Understanding Node.js
    1. Variables
      1. Numbers
      2. Boolean
      3. Arrays
      4. Object Literals
    2. Functions
      1. Functions 101
      2. Immediately Executing Function
      3. Anonymous Function
      4. Higher-Order Functions
    3. Closures
    4. Understanding Node.js Performance
      1. The I/O Scaling Problem
      2. Traditional Web Servers Using a Process Per Request
      3. Traditional Web Servers Using a Thread Pool
      4. The Nginx Way
      5. Node.js Performance Secret
    5. More Node.js Internals
      1. Thread Starvation
      2. Data-Intensive Applications
      3. The V8 JavaScript Engine
    6. More JavaScript
      1. Everything Is a Reference
      2. Default Values
      3. Exact Equality
      4. null
      5. Truthy and Falsy
      6. Revealing Module Pattern
      7. Understanding this
      8. Understanding Prototype
      9. Error Handling
    7. Summary
    8. Works Cited
  13. Chapter 3: Core Node.js
    1. Node.js File-Based Module System
      1. Node.js require Function
      2. Node.js Exports
      3. Modules Best Practices
    2. Important Globals
      1. console
      2. Timers
      3. __filename and __dirname
      4. process
      5. Buffer
      6. global
    3. Core Modules
      1. Consuming Core Modules
      2. Path Module
      3. fs Module
      4. os Module
      5. util Module
    4. Reusing Node.js Code in the Browser
      1. Introducing AMD
      2. Setting Up RequireJS
      3. Playing with AMD
      4. Converting Node.js Code into Browser Code
    5. Summary
  14. Chapter 4: Node.js Packages
    1. Revisiting Node Modules
      1. Scanning for node_modules
      2. Folder-Based Modules
      3. Advantages of node_modules
      4. Module Caching and node_modules
    2. JSON
      1. Beginner’s Introduction to JSON
      2. Loading JSON in Node.js
      3. The JSON Global
    3. NPM
      1. package.json
      2. Installing an NPM Package
      3. Saving Dependencies
      4. Refresh the node_modules Folder
      5. Listing All Dependencies
      6. Removing a Dependency
      7. package.json Online Dependency Tracking
    4. Semantic Versioning
      1. Semantic Versioning in NPM / package.json
      2. Updating Dependencies
    5. Global Node.js Packages
      1. Using require with Global Modules
    6. Package.json and require
    7. Modules Recap
    8. Popular Node.js Packages
      1. Underscore
      2. Handling Command Line Arguments
      3. Handling Date/Time Using Moment
      4. Customizing Console Colors
    9. Additional Resources
    10. Summary
  15. Chapter 5: Events and Streams
    1. Classical Inheritance in JavaScript
      1. Arriving at an Inheritance Pattern
      2. The Constructor Property
      3. The Proper Node.js Way
      4. Overriding Functions in Child Classes
      5. Checking Inheritance Chain
      6. Deeper Understanding of the Internals of util.inherits
    2. Node.js Events
      1. EventEmitter class
      2. Creating Your Own Event Emitters
      3. Process Events
    3. Streams
      1. Pipe
      2. Consuming Readable Streams
      3. Writing to Writable Streams
      4. Creating Your Own Stream
    4. Summary
  16. Chapter 6: Getting Started with HTTP
    1. Basics of Node.js HTTP
      1. Inspecting Headers
      2. Using a Debugging Proxy
      3. Key Members of the Response Stream
      4. Key Members of the Request Stream
    2. Creating Your Own File Web Server
      1. Serving Base HTML
      2. Serving a Directory
    3. Introducing Connect
      1. Creating a Bare-Bones Connect Application
      2. Creating a Connect Middleware
      3. Mounting Middleware by Path Prefix
      4. Using an Object as Middleware
      5. Creating Configurable Middleware
      6. The Power of Chaining
    4. HTTPS
      1. Asymmetric Cryptography
      2. Generating Keys
      3. Create an HTTPS Server
      4. Use HTTPS by Default
    5. Summary
  17. Chapter 7: Introducing Express
    1. Basics of Express
    2. Popular Connect/ExpressJS Middleware
      1. Serving Static Pages
      2. Listing Directory Contents
      3. Accepting JSON Requests and HTML Form Inputs
      4. Handling Cookies
      5. Cookie-Based Sessions
      6. Compression
      7. Time-out Hanging Requests
    3. Express Response Object
      1. Simplifying Send
    4. Express Request Object
      1. Making Request and Response Cross Visible
    5. Understanding REST
    6. Express Application Routes
      1. Creating a Route Object
      2. A Deeper Look at the Path Option
      3. Parameter-Based Routing
      4. Express Router Object
    7. Additional Resources
    8. Summary
  18. Chapter 8: Persisting Data
    1. Introduction to NoSQL
      1. What Is a Document Database?
      2. What Is a Key-Value Store?
      3. Why NoSQL?
    2. Installing MongoDB
      1. Understanding the Binaries
      2. Running Your First Server
      3. The MongoDB REPL
    3. Important MongoDB Concepts
      1. MongoDB _id field
      2. MongoDB Document Format
    4. MongoDB Using Node.js
      1. Update a Document
      2. Update Operators
    5. Mongoose ODM
      1. Connecting to MongoDB
      2. Mongoose Schema and Model
    6. Using a MongoDB as a Distributed Session Store
    7. Managing MongoDB
    8. Additional Resources
    9. Summary
  19. Chapter 9: Front-End Basics
    1. What Is a SPA?
    2. Why AngularJS?
    3. Introduction to Twitter Bootstrap
    4. Set Up a Simple AngularJS Application
      1. Modules in AngularJS
      2. Directives in AngularJS
      3. Controller and $scope
    5. Creating a Simple To-Do List Application
      1. Creating a REST API
      2. Wire Up the Front End with the REST API
    6. Next Steps
    7. Additional Resources
    8. Summary
  20. Chapter 10: Simplifying Callbacks
    1. The Callback Hell
      1. If/else in an Async World
      2. Loops in an Async World
      3. Error Handling
    2. Introduction to Promises
      1. Create a Promise
      2. Promise States
      3. The Then and Catch Basics
      4. Chain-ability of Then
    3. Converting Callbacks to Promises
      1. Interfacing with nodeback
      2. Converting Non-nodeback Callback Functions
    4. Providing a Promise + nodeback Interface
    5. Further Notes on the Promise API
      1. Promises Supports Other Promises as a Value
      2. Ungracefully Terminating a Promise Chain (Intentionally)
      3. Promise Library Compatibility
      4. Inspecting the State of Promises
      5. Parallel Flow Control
    6. Generators
      1. Motivation for Generators
      2. Power of Generators in JavaScript
    7. Promises and Generators
      1. The Future
    8. Additional Resources
    9. Summary
  21. Chapter 11: Debugging
    1. The Console Object
      1. Simple Logging
      2. Simple Benchmark
      3. A Quick Way to Get the Call Stack
      4. Print to stderr
    2. The Debugger Statement
    3. Node’s Built-in Debugger
    4. Node-inspector
    5. Remote Debugging Node.js
      1. Remote Debugging with Node-inspector
    6. Node.js Configuration in WebStorm
    7. Additional Resources
    8. Summary
  22. Chapter 12: Testing
    1. Assert
    2. Mocha
      1. Installing Mocha
      2. Creating and Running a Basic Test
      3. Mocha API
      4. Async Testing
      5. Mocha Command Line Options
    3. Chai
      1. Additional Assertions in Chai
      2. Chai Plug-ins
      3. BDD Style Assertions
    4. Additional Resources
  23. Chapter 13: Deployment and Scalability
    1. Ensuring Uptime
      1. Using Forever
    2. Node.js Clustering
      1. Spawning Workers
      2. The Ideal Worker Count
      3. Handling HTTP Requests in Workers
      4. Communicating with the Master
    3. Core AWS Concepts
      1. Why Amazon Web Services
      2. AWS Console
      3. EC2
      4. Which EC2 AMI?
      5. EC2 Instance Types
      6. EBS
      7. Key Pair
      8. VPC
    4. Creating Your First EC2 Instance
      1. Create a Key Pair
      2. Configuring the Security Group
      3. Provisioning an Instance
    5. Connecting to the EC2 Instance for the First Time
      1. Setting Up a SSH Connect Script
      2. Keeping Your Server Safe
      3. Install Node.js and NPM
      4. Allowing Global Package Installation
      5. Running Your Node.js Application in AWS
    6. Additional Resources
    7. Summary
  24. Index