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.js, Second Edition

Book Description

Learning Node.js brings together the knowledge and JavaScript code needed to build master the Node.js platform and build server-side applications with extraordinary speed and scalability.

You’ll start by installing and running Node.js, understanding the extensions it uses, and quickly writing your first app. Next, building on the basics, you’ll write more capable application servers and extend them with today’s most powerful Node.js tools and modules. Finally, you’ll discover today’s best practices for testing, running Node.js code on production servers, and writing command-line utilities.

Throughout the book, author Marc Wandschneider teaches by walking the reader line-by-line through carefully crafted examples, demonstrating proven techniques for creating highly efficient applications and servers.

The second edition updates coverage of Node.js to reflect changes in the technology and how it is used in the three years since the first edition was published:

  •  Explanation of Node's new versioning scheme

  •  Updated coverage of Streams

  •  New coverage of installation using pre-build installers rather than from source code

  •  New coverage of Mongoose in the section on MongoDB

  •  New information about deploying Node on Heroku and Azure

  •  Expansion of coverage on testing

  • If you're a developer who wants to build server-side web applications with Node.js, Learning Node.js is your fatest route to success.

  • Build Node.js solutions that leverage current JavaScript skills 

  • Master Node.js nonblocking IO and async programming

  • Handle more requests and increase an application’s flexibility

  • Use and write modules

  • Perform common JSON/web server tasks

  • Use browsers to generate pages on the fly via Ajax calls and template libraries

  • Simplify development with the Express framework

  • Create database back-ends using popular NoSQL and relational databases

  • Deploy and run Node.js apps on Unix/macOS or Windows servers

  • Deploy apps to Heroku and Microsoft Azure

  • Support virtual hosts and SSL/HTTPS security

  • Test Node.js programs that mix synchronous, async, and RESTful server API functionality

  • Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents at a Glance
    6. Contents
    7. Acknowledgments
    8. About the Author
    9. Introduction
      1. Why Node.js?
        1. The Web
        2. New Technologies
      2. What Exactly Is Node.js?
      3. Who Is This Book For?
      4. How to Use this Book
      5. Download the Source Code
    10. Part I: Learning to Walk
      1. 1. Getting Started
        1. Installing Node.js
          1. Installation on Windows
          2. Installation on the Mac
          3. Installation on Linux
        2. Running Node.js and “Hello World!”
          1. The Node Shell
          2. Editing and Running JavaScript Files
        3. Your First Web Server
        4. Debugging Your Node.js Programs
        5. Staying Up-to-Date and Finding Help
        6. Summary
      2. 2. A Closer Look at JavaScript
        1. Types
          1. Type Basics
          2. Constants
          3. Numbers
          4. Booleans
          5. Strings
          6. Objects
          7. Arrays
        2. Type Comparisons and Conversions
        3. Functions
          1. Basics
          2. Function Scope
          3. Arrow Functions
        4. Language Constructs
        5. Classes, Prototypes, and Inheritance
          1. Prototypes and Inheritance
        6. Errors and Exceptions
        7. Some Important Node.js Globals
          1. global
          2. console
          3. process
        8. Summary
      3. 3. Asynchronous Programming
        1. The Old Way of Doing Things
        2. The Node.js Way of Doing Things
        3. Error Handling and Asynchronous Functions
          1. The callback Function and Error Handling
        4. Who Am I? Maintaining a Sense of Identity
        5. Being Polite—Learning to Give Up Control
        6. Synchronous Function Calls
        7. Summary
    11. Part II: Learning to Run
      1. 4. Writing Simple Applications
        1. Your First JSON Server
          1. Returning Some Data
        2. Node Pattern: Asynchronous Loops
        3. Learning to Juggle: Handling More Requests
        4. More on the Request and Response Objects
        5. Increased Flexibility: GET Params
        6. Modifying Things: POST Data
          1. Receiving JSON POST Data
          2. Receiving Form POST Data
        7. Summary
      2. 5. Modules
        1. Writing Simple Modules
          1. Modules and Objects
        2. npm: The Node Package Manager
        3. Consuming Modules
          1. Searching for Modules
          2. Module Caching
          3. Cycles
        4. Writing Modules
          1. Creating Your Module
          2. Developing with Your Module
          3. Publishing Your Modules
        5. Managing Asynchronous Code
          1. The Problem
          2. Our Preferred Solution—async
          3. Making Promises and Keeping Them
        6. Summary
      3. 6. Expanding Your Web Server
        1. Serving Static Content with Streams
          1. Reading a File
          2. Serving Static Files in a Web Server with Buffers
          3. Serving Up More Than Just HTML
        2. Assembling Content on the Client: Templates
          1. The HTML Skeleton Page
          2. Serving Static Content
          3. Modifying Your URL Scheme
          4. The JavaScript Loader/Bootstrapper
          5. Templating with Mustache
          6. Your Home Page Mustache Template
          7. Putting It All Together
        3. Summary
    12. Part III: Writing Web Applications
      1. 7. Building Web Applications with Express
        1. Installing Express
          1. Hello World in Express
        2. Routing and Layers in Express
          1. Routing Basics
          2. Updating Your Photo Album App for Routing
        3. REST API Design and Modules
          1. API Design
          2. Modules
        4. Additional Middleware Functionality
          1. Usage
          2. Configurations
          3. Ordering of Middleware
          4. Static File Handling
          5. POST Data, Cookies, and Sessions
          6. Better Browser Support for PUT and DELETE
          7. Compressing Output
        5. Adding Authentication to our Application
          1. Getting Started
          2. Laying Down the Plumbing
          3. Creating a Login Form
          4. Logging the User In
          5. Restricting Access to a Page
          6. Flash Messages
          7. Running the Sample
        6. Error Handling
        7. Summary
      2. 8. Databases I: NoSQL (MongoDB)
        1. Setting Up MongoDB
          1. Installing MongoDB
          2. Using Mongo DB in Node.js
        2. Structuring Your Data for MongoDB
          1. It’s All JavaScript
          2. Data Types
        3. Understanding the Basic Operations
          1. Connecting and Creating a Database
          2. Creating Collections
          3. Inserting Documents into Collections
          4. Updating Document Values
          5. Deleting Documents from Collections
          6. Querying Collections
          7. Seeing it all in Action
        4. Updating Your Photo Albums App
          1. Writing the Low-Level Operations
          2. Modifying the API for the JSON Server
          3. Updating Your Handlers
          4. Adding Some New Pages to the Application
        5. Recapping the App Structure
        6. Summary
      3. 9. Databases II: SQL (MySQL)
        1. Getting Ready
          1. Installing MySQL
          2. Adding the mysql Module from npm
        2. Creating a Schema for the Database
        3. Basic Database Operations
          1. Connecting
          2. Adding Queries
          3. Updating the Photo Sharing Application
        4. Authenticating via the Database
          1. Updating the API to Support Users
          2. Examining the Core User Data Operations
          3. Updating the Express Application for Authentication
          4. Implementing User Authentication
          5. Creating the User Handler
          6. Hooking up Passport and Routes
          7. Creating the Login and Register Pages
        5. Summary
    13. Part IV: Getting the Most Out of Node.js
      1. 10. Deployment and Development I: Rolling Your Own
        1. Deployment
          1. Level: Basic
          2. Level: Ninja
        2. Multiprocessor Deployment: Using a Proxy
          1. Multiple Servers and Sessions
        3. Virtual Hosting
          1. Express Support
        4. Securing Your Projects with HTTPS/SSL
          1. Generating Test Certificates
          2. Built-in Support
          3. Proxy Server Support
        5. Multiplatform Development
          1. Locations and Configuration Files
          2. Handling Path Differences
        6. Summary
      2. 11. Deployment and Development II: Heroku and Azure
        1. Deploying to Heroku
          1. Before We Begin
          2. Preparing Your Deployment
          3. Create and Deploy the Application on Heroku
          4. We Have a Problem
          5. And That’s It!
        2. Deploying to Microsoft Azure
          1. Before We Begin
          2. Preparing Your Deployment
          3. Create and Deploy the Application on Azure
          4. We Have a Problem (and Déja Vu!)
          5. And That’s It!
        3. Summary
      3. 12. Command-Line Programming
        1. Running Command-Line Scripts
          1. UNIX and Mac
          2. Windows
          3. Scripts and Parameters
        2. Working with Files Synchronously
          1. Basic File APIs
          2. Files and Stats
          3. Listing Contents of Directories
        3. Interacting with the User: stdin/stdout
          1. Basic Buffered Input and Output
          2. Unbuffered Input
          3. The readline Module
        4. Working with Processes
          1. Simple Process Creation
          2. Advanced Process Creation with Spawn
        5. Summary
      4. 13. Testing
        1. Choosing a Framework
          1. Installing Nodeunit
        2. Writing Tests
          1. Simple Functional Tests
          2. Groups of Tests
          3. Testing Asynchronous Functionality
        3. API Testing
        4. Summary
    14. Index
    15. Code Snippets