Practical Node.js: Building Real-World Scalable Web Apps

Book description


Practical Node.js is your step-by-step guide to learning how to build a wide range of scalable real-world web applications using a professional development toolkit. Node.js is an innovative and highly efficient platform for creating web services. But Node.js doesn't live in a vacuum! In a modern web development, many different components need to be put together routing, database driver, ORM, session management, OAuth, HTML template engine, CSS compiler and many more.

If you already know the basics of Node.js, now is the time to discover how to bring it to production level by leveraging its vast ecosystem of packages. As a web developer, you'll work with a varied collection of standards and frameworks Practical Node.js shows you how all those pieces fit together.

Practical Node.js takes you from installing all the necessary modules to writing full-stack web applications by harnessing the power of the Express.js and Hapi frameworks, the MongoDB database with Mongoskin and Mongoose, Jade and Handlebars template engines, Stylus and LESS CSS languages, OAuth and Everyauth libraries, and the Socket.IO and Derby libraries, and everything in between. The book also covers how to deploy to Heroku and AWS, daemonize apps, and write REST APIs. You'll build full-stack real-world Node.js apps from scratch, and also discover how to write your own Node.js modules and publish them on NPM. You already know what Node.js is; now learn what you can do with it and how far you can take it!


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 Node.js and Other Essentials
    1. Installing Node.js and NPM
      1. One-Click Installers
      2. Installing with HomeBrew or MacPorts
      3. Installing from a Tar File
      4. Installing Without sudo
      5. Installing from a Git Repo
      6. Multiversion Setup with Nave
      7. Multiversion Setup with NVM
      8. Alternative Multiversion Systems
      9. Checking the Installation
      10. Node.js Console (REPL)
    2. Launching Node.js Scripts
    3. Node.js Basics and Syntax
      1. Loose Typing
      2. Buffer—Node.js Super Data Type
      3. Object Literal Notation
      4. Functions
      5. Arrays
      6. Prototypal Nature
      7. Conventions
      8. Node.js Globals and Reserved Keywords
      9. __dirname vs. process.cwd
      10. Browser Application Programming Interface Helpers
      11. Node.js Core Modules
      12. Handy Node.js Utilities
      13. Reading to and Writing from the File System in Node.js
      14. Streaming Data in Node.js
      15. Installing Node.js Modules with NPM
      16. Taming Callbacks in Node.js
      17. Hello World Server with HTTP Node.js Module
    4. Debugging Node.js Programs
      1. Core Node.js Debugger
      2. Debugging with Node Inspector
    5. Node.js IDEs and Code Editors
    6. Watching for File Changes
    7. Summary
  12. Chapter 2: Using Express.js 4 to Create Node.js Web Apps
    1. What Is Express.js?
    2. How Express.js Works
    3. Express.js Installation
      1. Express.js Version
      2. Express.js Generator
      3. Local Express.js
    4. Express.js Scaffolding
      1. Express.js Command-Line Interface
      2. Routes in Express.js
      3. Middleware as the Backbone of Express.js
      4. Configuration of an Express.js App
      5. Jade Is Haml for Express.js/Node.js
      6. Conclusion About Scaffolding
    5. The Blog Project Overview
      1. Submitting the Data
    6. Express.js 4 Hello World Example
      1. Setting up Folders
      2. NPM Init and package.json
      3. Dependency Declaration: npm install
      4. The App.js File
      5. Meet Jade: One Template to Rule Them All
      6. Running the Hello World App
    7. Summary
  13. Chapter 3: TDD and BDD for Node.js with Mocha
    1. Installing and Understanding Mocha
      1. Understanding Mocha Hooks
    2. TDD with the Assert
      1. Chai Assert
    3. BDD with Expect.js
      1. Expect.js Syntax
    4. Project: Writing the First BDD Test for Blog
      1. Putting Configs into a Makefile
    5. Summary
  14. Chapter 4: Template Engines: Jade and Handlebars
    1. Jade Syntax and Features
      1. Tags
      2. Variables/Locals
      3. Attributes
      4. Literals
      5. Text
      6. Script and Style Blocks
      7. JavaScript Code
      9. Conditions (if)
      10. Iterations (each loops)
      11. Filters
      12. Interpolation
      13. Case
      14. Mixins
      15. Include
      16. Extend
    2. Standalone Jade Usage
    3. Handlebars Syntax
      1. Variables
      2. Iteration (each)
      3. Unescaped Output
      4. Conditions (if)
      5. Unless
      6. With
      8. Custom Helpers
      9. Includes (Partials)
    4. Standalone Handlebars Usage
    5. Jade and Handlebars Usage in Express.js 4
      1. Jade and Express.js
      2. Handlebars and Express.js
    6. Project: Adding Jade Templates to Blog
      1. layout.jade
      2. index.jade
      3. article.jade
      4. login.jade
      5. post.jade
      6. admin.jade
    7. Summary
  15. Chapter 5: Persistence with MongoDB and Mongoskin
      1. Easy and Proper Installation of MongoDB
    1. How to Run the Mongo Server
    2. Data Manipulation from the Mongo Console
    3. MongoDB Shell in Detail
    4. Minimalistic Native MongoDB Driver for Node.js Example
    5. Main Mongoskin Methods
    6. Project: Storing Blog Data in MongoDB with Mongoskin
      1. Project: Adding MongoDB Seed Data
      2. Project: Writing Mocha Tests
      3. Project: Adding Persistence
      4. Running the App
    7. Summary
  16. Chapter 6: Using Sessions and OAuth to Authorize and Authenticate Users in Node.js Apps
    1. Authorization with Express.js Middleware
    2. Token-Based Authentication
    3. Session-Based Authentication
    4. Project: Adding E-mail and Password Login to Blog
      1. Session Middleware
      2. Authorization in Blog
      3. Authentication in Blog
      4. Running the App
    5. Node.js OAuth
      1. Twitter OAuth 2.0 Example with Node.js OAuth
      2. Everyauth
    6. Project: Adding Twitter OAuth 1.0 Sign-in to Blog with Everyauth
      1. Adding a Sign-in with a Twitter Link
      2. Configuring the Everyauth Twitter Strategy
    7. Summary
  17. Chapter 7: Boosting Your Node.js Data with the Mongoose ORM Library
    1. Mongoose Installation
    2. Connection Establishment in a Standalone Mongoose Script
    3. Mongoose Schemas
    4. Hooks for Keeping Code Organized
    5. Custom Static and Instance Methods
    6. Mongoose Models
    7. Relationships and Joins with Population
    8. Nested Documents
    9. Virtual Fields
    10. Schema Type Behavior Amendment
    11. Express.js + Mongoose = True MVC
    12. Summary
  18. Chapter 8: Building Node.js REST API Servers with Express.js and Hapi
    1. RESTful API Basics
    2. Project Dependencies
    3. Test Coverage with Mocha and Superagent
    4. REST API Server Implementation with Express and Mongoskin
    5. Refactoring: Hapi RESP API Server
    6. Summary
  19. Chapter 9: Real-Time Apps with WebSocket, Socket.IO, and DerbyJS
    1. What Is WebSocket?
    2. Native WebSocket and Node.js with the ws Module Example
      1. Browser WebSocket Implementation
      2. Node.js Server with ws Module Implementation
    3. Socket.IO and Express.js Example
    4. Collaborative Online Code Editor Example with DerbyJS, Express.js, and MongoDB
      1. Project Dependencies and package.json
      2. Server-side Code
      3. DerbyJS App
      4. DerbyJS View
      5. Editor Tryout
    5. Summary
  20. Chapter 10: Getting Node.js Apps Production Ready
    1. Environment Variables
    2. Express.js in Production
    3. Socket.IO in Production
    4. Error Handling
    5. Node.js Domains for Error Handling
    6. Multithreading with Cluster
    7. Multithreading with Cluster2
    8. Event Logging and Monitoring
      1. Monitoring
      2. REPL in Production
      3. Winston
      4. Papertrail App for Logging
    9. Building Tasks with Grunt
    10. Git for Version Control and Deployments
      1. Installing Git
      2. Generating SSH Keys
      3. Creating a Local Git Repository
      4. Pushing the Local Repository to GitHub
    11. Running Tests in Cloud with TravisCI
      1. TravisCI Configuration
    12. Summary
  21. Chapter 11: Deploying Node.js Apps
    1. Deploying to Heroku
    2. Deploying to Amazon Web Services
    3. Keeping Node.js Apps Alive with forever, Upstart, and init.d
      1. forever
      2. Upstart Scripts
      3. init.d
    4. Serving Static Resources Properly with Nginx
    5. Caching with Varnish
    6. Summary
  22. Chapter 12: Publishing Node.js Modules and Contributing to Open Source
    1. Recommended Folder Structure
    2. Required Patterns
    3. package.json
    4. Publishing to NPM
    5. Locking Versions
    6. Summary
    7. Practical Node.js Conclusion
    8. Further Reading
    9. Errata and Contacts
  23. Index

Product information

  • Title: Practical Node.js: Building Real-World Scalable Web Apps
  • Author(s): Azat Mardan
  • Release date: July 2014
  • Publisher(s): Apress
  • ISBN: 9781430265962