O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Node

Book Description

Take your web development skills from browser to server with Node—and learn how to write fast, highly scalable network applications on this JavaScript-based platform. With this hands-on guide, you’ll quickly master Node’s core fundamentals, gain experience with several built-in and contributed modules, and learn the differences and parallels between client- and server-side programming.

Get up to speed on Node’s event-driven, asynchronous I/O model for developing data-intensive applications that are frequently accessed but computationally simple. If you’re comfortable working with JavaScript, this book provides numerous programming and deployment examples to help you take advantage of server-side development with Node.

  • Explore Node’s unique approach to asynchronous development
  • Build sample Node applications with the Express framework and Connect middleware
  • Use NoSQL solutions such as Redis and MongoDB—and explore Node’s relational database modules
  • Work with PDF files, serve HTML5 media, and create graphics with Canvas
  • Set up bidirectional communication between browser and server with WebSockets
  • Learn in-depth practices for debugging and testing your applications
  • Deploy Node applications in the cloud or on your own system

"Learning Node will make it easy for someone from any programming background to get a grip on Node.js and build amazing projects."

—Tom Hughes-Croucher, co-author of Node: Up and Running (O’Reilly)

Table of Contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Not Your Ordinary JavaScript
    2. Why Node?
    3. This Book’s Intended Audience
    4. How to Best Use This Book
    5. The Technology
    6. The Examples
    7. Conventions Used in This Book
    8. Using Code Examples
    9. Safari® Books Online
    10. How to Contact Us
    11. Acknowledgments
  3. 1. Node.js: Up and Running
    1. Setting Up a Node Development Environment
      1. Installing Node on Linux (Ubuntu)
      2. Partnering Node with WebMatrix on Windows 7
      3. Updating Node
    2. Node: Jumping In
      1. Hello, World in Node
      2. Hello, World from the Top
    3. Asynchronous Functions and the Node Event Loop
      1. Reading a File Asynchronously
      2. Taking a Closer Look at Asynchronous Program Flow
    4. Benefits of Node
  4. 2. Interactive Node with REPL
    1. REPL: First Looks and Undefined Expressions
    2. Benefits of REPL: Getting a Closer Understanding of JavaScript Under the Hood
    3. Multiline and More Complex JavaScript
      1. REPL Commands
      2. REPL and rlwrap
      3. Custom REPL
    4. Stuff Happens—Save Often
  5. 3. The Node Core
    1. Globals: global, process, and Buffer
      1. global
      2. process
      3. Buffer
    2. The Timers: setTimeout, clearTimeout, setInterval, and clearInterval
    3. Servers, Streams, and Sockets
      1. TCP Sockets and Servers
      2. HTTP
      3. UDP/Datagram Socket
      4. Streams, Pipes, and Readline
    4. Child Processes
      1. child_process.spawn
      2. child_process.exec and child_process.execFile
      3. child_process.fork
      4. Running a Child Process Application in Windows
    5. Domain Resolution and URL Processing
    6. The Utilities Module and Object Inheritance
    7. Events and EventEmitter
  6. 4. The Node Module System
    1. Loading a Module with require and Default Paths
    2. External Modules and the Node Package Manager
    3. Finding Modules
      1. Colors: Simple Is Best
      2. Optimist: Another Short and Simple Module
      3. Underscore
    4. Creating Your Own Custom Module
      1. Packaging an Entire Directory
      2. Preparing Your Module for Publication
      3. Publishing the Module
  7. 5. Control Flow, Asynchronous Patterns, and Exception Handling
    1. Promises, No Promises, Callback Instead
    2. Sequential Functionality, Nested Callbacks, and Exception Handling
    3. Asynchronous Patterns and Control Flow Modules
      1. Step
      2. Async
    4. Node Style
  8. 6. Routing Traffic, Serving Files, and Middleware
    1. Building a Simple Static File Server from Scratch
    2. Middleware
      1. Connect Basics
      2. Connect Middleware
        1. connect.static
        2. connect.logger
        3. connect.parseCookie and connect.cookieSession
      3. Custom Connect Middleware
    3. Routers
    4. Proxies
  9. 7. The Express Framework
    1. Express: Up and Running
    2. The app.js File in More Detail
    3. Error Handling
    4. A Closer Look at the Express/Connect Partnership
    5. Routing
      1. Routing Path
      2. Routing and HTTP Verbs
    6. Cue the MVC
    7. Testing the Express Application with cURL
  10. 8. Express, Template Systems, and CSS
    1. The Embedded JavaScript (EJS) Template System
      1. Learning the Basic Syntax
      2. Using EJS with Node
      3. Using the EJS for Node Filters
    2. Using a Template System (EJS) with Express
      1. Restructuring for a Multiple Object Environment
      2. Routing to Static Files
      3. Processing a New Object Post
      4. Working with the Widgets Index and Generating a Picklist
      5. Showing an Individual Object and Confirming an Object Deletion
      6. Providing an Update Form and Processing a PUT Request
    3. The Jade Template System
      1. Taking the Nickel Tour of the Jade Syntax
      2. Using block and extends to Modularize the View Templates
      3. Converting the Widget Views into Jade Templates
        1. Converting the main widgets display view
        2. Converting the edit and deletion forms
    4. Incorporating Stylus for Simplified CSS
  11. 9. Structured Data with Node and Redis
    1. Getting Started with Node and Redis
    2. Building a Game Leaderboard
    3. Creating a Message Queue
    4. Adding a Stats Middleware to an Express Application
  12. 10. Node and MongoDB: Document-Centric Data
    1. The MongoDB Native Node.js Driver
      1. Getting Started with MongoDB
      2. Defining, Creating, and Dropping a MongoDB Collection
      3. Adding Data to a Collection
      4. Querying the Data
      5. Using Updates, Upserts, and Find and Remove
    2. Implementing a Widget Model with Mongoose
      1. Refactoring the Widget Factory
      2. Adding the MongoDB Backend
  13. 11. The Node Relational Database Bindings
    1. Getting Started with db-mysql
      1. Using Query String or Chained Methods
      2. Updating the Database with Direct Queries
      3. Updating the Database with Chained Methods
    2. Native JavaScript MySQL Access with node-mysql
      1. Basic CRUD with node-mysql
      2. MySQL Transactions with mysql-queues
    3. ORM Support with Sequelize
      1. Defining a Model
      2. Using CRUD, ORM Style
      3. Adding Several Objects Easily
      4. Overcoming Issues Related to Going from Relational to ORM
  14. 12. Graphics and HTML5 Video
    1. Creating and Working with PDFs
      1. Accessing PDF Tools with Child Processes
        1. Taking page snapshots with wkhtmltopdf
        2. Accessing data about a PDF file with PDF Toolkit
        3. Creating a PDF uploader and dealing with graphics lag time
      2. Creating PDFs with PDFKit
    2. Accessing ImageMagick from a Child Process
    3. Properly Serving HTML5 Video with HTTP
    4. Creating and Streaming Canvas Content
  15. 13. WebSockets and Socket.IO
    1. WebSockets
    2. An Introduction to Socket.IO
      1. A Simple Communication Example
      2. WebSockets in an Asynchronous World
      3. About That Client Code
    3. Configuring Socket.IO
    4. Chat: The WebSockets “Hello, World”
    5. Using Socket.IO with Express
  16. 14. Testing and Debugging Node Applications
    1. Debugging
      1. The Node.js Debugger
      2. Client-Side Debugging with Node Inspector
    2. Unit Testing
      1. Unit Testing with Assert
      2. Unit Testing with Nodeunit
      3. Other Testing Frameworks
        1. Mocha
        2. Jasmine
        3. Vows
    3. Acceptance Testing
      1. Selenium Testing with Soda
      2. Emulating a Browser with Tobi and Zombie
    4. Performance Testing: Benchmarks and Load Tests
      1. Benchmark Testing with ApacheBench
      2. Load Testing with Nodeload
    5. Refreshing Code with Nodemon
  17. 15. Guards at the Gate
    1. Encrypting Data
      1. Setting Up TSL/SSL
      2. Working with HTTPS
      3. Safely Storing Passwords
    2. Authentication/Authorization with Passport
      1. Authorization/Authentication Strategies: OAuth, OpenID, Username/Password Verification
      2. The Local Passport Strategy
      3. The Twitter Passport Strategy (OAuth)
    3. Protecting Applications and Preventing Attacks
      1. Don’t Use eval
      2. Do Use Checkboxes, Radio Buttons, and Drop-Down Selections
      3. Scrub Your Data and Sanitize It with node-validator
    4. Sandboxed Code
  18. 16. Scaling and Deploying Node Applications
    1. Deploying Your Node Application to Your Server
      1. Writing That package.json File
      2. Keeping Your Application Alive with Forever
      3. Using Node and Apache Together
      4. Improving Performance
    2. Deployment to a Cloud Service
      1. Deploying to Windows Azure via Cloud9 IDE
      2. Joyent Development SmartMachines
      3. Heroku
      4. Amazon EC2
      5. Nodejitsu
  19. A. Node, Git, and GitHub
  20. Index
  21. About the Author
  22. Colophon
  23. Copyright