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

Node.js, MongoDB, and AngularJS Web Development

Book Description

Node.js, MongoDB and AngularJS Web Development
The definitive guide to building JavaScript-based Web applications from server to browser

Node.js, MongoDB, and AngularJS are three new web development technologies that together provide an easy to implement, fully integrated web development stack. Node.js is a leading server-side programming environment, MongoDB is the most popular NoSQL database, and AngularJS is quickly becoming the leading framework for MVC-based front-end development. Together they allow web programmers to create high-performance sites and applications built completely in JavaScript, from server to client.

Node.js, MongoDB and AngularJS Web Development is a complete guide for web programmers who want to integrate these three technologies into full working solutions. It begins with concise, crystal-clear tutorials on each of the three technologies and then quickly moves on to building several common web applications.

Readers will learn how to use Node.js and MongoDB to build more scalable, high-performance sites, how to leverage AngularJS's innovative MVC approach to structure more effective pages and applications, and how to use all three together to deliver outstanding next-generation Web solutions.

  • Implement a highly scalable and dynamic web server using Node.js and Express

  • Build server-side web services in JavaScript

  • Implement a MongoDB data store for your web applications

  • Access and interact with MongoDB from Node.js JavaScript code

  • Define static and dynamic web routes and implement server-side scripts to support them

  • Implement Express in Node.js

  • Create Jade templates

  • Define your own custom AngularJS directives that extend the HTML language

  • Implement client-side services that can interact with the Node.js web server

  • Build dynamic browser views that provide rich user interaction

  • Add authenticated user accounts to your web applications

  • Add nested comment components to your web pages

  • Build an end-to-end shopping cart

  • Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Developer’s Library
    5. Dedication Page
    6. Contents
    7. Acknowledgments
    8. About the Author
    9. We Want to Hear from You!
    10. Reader Services
    11. Introduction
      1. Who Should Read This Book
      2. Why You Should Read This Book
      3. What You Will Learn from This Book
      4. What Is Node.js?
      5. What Is MongoDB?
      6. What Is AngularJS?
      7. How This Book Is Organized
      8. Getting the Code Examples
      9. A Final Word
      10. 1. Introducing the Node.js-to-AngularJS Stack
        1. Understanding the Basic Web Development Framework
          1. Users
          2. The Browser
          3. Webservers
          4. Backend Services
        2. Understanding the Node.js-to-AngularJS Stack Components
          1. Node.js
          2. MongoDB
          3. Express
          4. AngularJS
        3. Summary
        4. Up Next
      11. 2. JavaScript Primer
        1. Defining Variables
        2. Understanding JavaScript Data Types
        3. Using Operators
          1. Arithmetic Operators
          2. Assignment Operators
          3. Applying Comparison and Conditional Operators
        4. Implementing Looping
          1. while Loops
          2. do/while Loops
          3. for Loops
          4. for/in Loops
          5. Interrupting Loops
        5. Creating Functions
          1. Defining Functions
          2. Passing Variables to Functions
          3. Returning Values from Functions
          4. Using Anonymous Functions
        6. Understanding Variable Scope
        7. Using JavaScript Objects
          1. Using Object Syntax
          2. Creating Custom Defined Objects
          3. Using a Prototyping Object Pattern
        8. Manipulating Strings
          1. Combining Strings
          2. Searching a String for a Substring
          3. Replacing a Word in a String
          4. Splitting a String into an Array
        9. Working with Arrays
          1. Combining Arrays
          2. Iterating Through Arrays
          3. Converting an Array into a String
          4. Checking Whether an Array Contains an Item
          5. Adding Items to and Removing Items from Arrays
        10. Adding Error Handling
          1. try/catch Blocks
          2. Throwing Your Own Errors
          3. Using Finally
        11. Summary
        12. Up Next
    12. II: Learning Node.js
      1. 3. Getting Started with Node.js
        1. Understanding Node.js
          1. Who Uses Node.js?
          2. What Is Node.js Used For?
        2. Installing Node.js
          1. Looking at the Node.js Install Location
          2. Verifying Node.js Executables
          3. Selecting a Node.js IDE
        3. Working with Node.js Packages
          1. What Are Node Packaged Modules?
          2. Understanding the Node Package Registry
          3. Using the Node Package Manager
          4. Searching for Node Packaged Modules
          5. Installing Node Packaged Modules
          6. Using package.json
        4. Creating a Node.js Application
          1. Creating a Node.js Packaged Module
          2. Publishing a Node.js Packaged Module to the NPM Registry
          3. Using a Node.js Packaged Module in a Node.js Application
        5. Writing Data to the Console
        6. Summary
        7. Up Next
      2. 4. Using Events, Listeners, Timers, and Callbacks in Node.js
        1. Understanding the Node.js Event Model
          1. Comparing Event Callbacks and Threaded Models
          2. Blocking I/O in Node.js
          3. A Conversation Example
        2. Adding Work to the Event Queue
          1. Implementing Timers
          2. Using nextTick to Schedule Work
          3. Implementing Event Emitters and Listeners
        3. Implementing Callbacks
          1. Passing Additional Parameters to Callbacks
          2. Implementing Closure in Callbacks
          3. Chaining Callbacks
        4. Summary
        5. Up Next
      3. 5. Handling Data I/O in Node.js
        1. Working with JSON
          1. Converting JSON to JavaScript Objects
          2. Converting JavaScript Objects to JSON
        2. Using the Buffer Module to Buffer Data
          1. Understanding Buffered Data
          2. Creating Buffers
          3. Writing to Buffers
          4. Reading from Buffers
          5. Determining Buffer Length
          6. Copying Buffers
          7. Slicing Buffers
          8. Concatenating Buffers
        3. Using the Stream Module to Stream Data
          1. Readable Streams
          2. Writable Streams
          3. Duplex Streams
          4. Transform Streams
          5. Piping Readable Streams to Writable Streams
        4. Compressing and Decompressing Data with Zlib
          1. Compressing and Decompressing Buffers
          2. Compressing/Decompressing Streams
        5. Summary
        6. Up Next
      4. 6. Accessing the File System from Node.js
        1. Synchronous Versus Asynchronous File System Calls
        2. Opening and Closing Files
        3. Writing Files
          1. Simple File Writing
          2. Synchronous File Writing
          3. Asynchronous File Writing
          4. Streaming File Writing
        4. Reading Files
          1. Simple File Reading
          2. Synchronous File Reading
          3. Asynchronous File Reading
          4. Streaming File Reading
        5. Other File System Tasks
          1. Verifying Path Existence
          2. Getting File Info
          3. Listing Files
          4. Deleting Files
          5. Truncating Files
          6. Making and Removing Directories
          7. Renaming Files and Directories
          8. Watching for File Changes
        6. Summary
        7. Up Next
      5. 7. Implementing HTTP Services in Node.js
        1. Processing URLs
          1. Understanding the URL Object
          2. Resolving the URL Components
        2. Processing Query Strings and Form Parameters
        3. Understanding Request, Response, and Server Objects
          1. The http.ClientRequest Object
          2. The http.ServerResponse Object
          3. The http.IncomingMessage Object
          4. The HTTP Server Object
        4. Implementing HTTP Clients and Servers in Node.js
          1. Serving Static Files
          2. Implementing Dynamic GET Servers
          3. Implementing POST Servers
          4. Interacting with External Sources
        5. Implementing HTTPS Servers and Clients
          1. Creating an HTTPS Client
          2. Creating an HTTPS Server
        6. Summary
        7. Up Next
      6. 8. Implementing Socket Services in Node.js
        1. Understanding Network Sockets
        2. Understanding TCP Server and Socket Objects
          1. The net.Socket Object
          2. The net.Server Object
        3. Implementing TCP Socket Servers and Clients
          1. Implementing a TCP Socket Client
          2. Implementing a TCP Socket Server
        4. Implementing TLS Servers and Clients
          1. Creating a TLS Socket Client
          2. Creating a TLS Socket Server
        5. Summary
        6. Up Next
      7. 9. Scaling Applications Using Multiple Processors in Node.js
        1. Understanding the process Module
          1. Understanding Process I/O Pipes
          2. Understanding Process Signals
          3. Controlling Process Execution with the process Module
          4. Getting Information from the process Module
        2. Implementing Child Processes
          1. Understanding the ChildProcess Object
          2. Executing a System Command on Another Process by Using exec()
          3. Executing an Executable File on Another Process Using execFile()
          4. Spawning a Process in Another Node.js Instance Using spawn()
          5. Implementing Child Forks
        3. Implementing Process Clusters
          1. Using the cluster Module
          2. Understanding the Worker Object
          3. Implementing an HTTP Cluster
        4. Summary
        5. Up Next
      8. 10. Using Additional Node.js Modules
        1. Using the os Module
        2. Using the util Module
          1. Formatting Strings
          2. Checking Object Types
          3. Synchronous Writing to Output Streams
          4. Converting JavaScript Objects to Strings
          5. Inheriting Functionality from Other Objects
          6. Using the dns Module
        3. Summary
        4. Up Next
    13. III: Learning MongoDB
      1. 11. Understanding NoSQL and MongoDB
        1. Why NoSQL?
        2. Understanding MongoDB
          1. Understanding Collections
          2. Understanding Documents
        3. MongoDB Data Types
        4. Planning Your Data Model
          1. Normalizing Data with Document References
          2. Denormalizing Data with Embedded Documents
          3. Using Capped Collections
          4. Understanding Atomic Write Operations
          5. Considering Document Growth
          6. Identifying Indexing, Sharding, and Replication Opportunities
          7. Large Collections Versus Large Numbers of Collections
          8. Deciding on Data Life Cycles
          9. Considering Data Usability and Performance
        5. Summary
        6. Up Next
      2. 12. Getting Started with MongoDB
        1. Building the MongoDB Environment
          1. Installing MongoDB
          2. Starting MongoDB
          3. Stopping MongoDB
          4. Accessing MongoDB from the Shell Client
        2. Administering User Accounts
          1. Listing Users
          2. Creating User Accounts
          3. Removing Users
        3. Configuring Access Control
          1. Creating a User Administrator Account
          2. Turning on Authentication
          3. Creating a Database Administrator Account
        4. Administering Databases
          1. Displaying a List of Databases
          2. Changing the Current Database
          3. Creating Databases
          4. Deleting Databases
          5. Copying Databases
        5. Managing Collections
          1. Displaying a List of Collections in a Database
          2. Creating Collections
          3. Deleting Collections
          4. Finding Documents in a Collection
          5. Adding Documents to a Collection
          6. Deleting Documents from a Collection
          7. Updating Documents in a Collection
        6. Summary
        7. Up Next
      3. 13. Getting Started with MongoDB and Node.js
        1. Adding the MongoDB Driver to Node.js
        2. Connecting to MongoDB from Node.js
          1. Understanding the Write Concern
          2. Understanding the Server Object
          3. Connecting to MongoDB via a Client Object
          4. Connecting to MongoDB via a Connection String
        3. Understanding the Objects Used in the MongoDB Node.js Driver
          1. Understanding the Db Object
          2. Understanding the Admin Object
          3. Understanding the Collection Object
          4. Understanding the Cursor Object
        4. Accessing and Manipulating Databases
          1. Listing Databases
          2. Creating a Database
          3. Deleting a Database
          4. Creating, Listing, and Deleting Databases Example
          5. Getting the Status of the MongoDB Server
        5. Accessing and Manipulating Collections
          1. Listing Collections
          2. Creating Collections
          3. Deleting Collections
          4. Collection Creation, Listing, and Deleting Example
          5. Getting Collection Information
        6. Summary
        7. Up Next
      4. 14. Manipulating MongoDB Documents from Node.js
        1. Understanding Database Change Options
        2. Understanding Database Update Operators
        3. Adding Documents to a Collection
        4. Getting Documents from a Collection
        5. Updating Documents in a Collection
        6. Atomically Modifying Documents in a Collection
        7. Saving Documents in a Collection
        8. Using upsert to Insert Documents in a Collection
        9. Deleting Documents from a Collection
        10. Removing a Single Document from a Collection
        11. Summary
        12. Up Next
      5. 15. Accessing MongoDB Documents from Node.js
        1. Introducing the Data Set
        2. Understanding Query Objects
        3. Understanding Query options Objects
        4. Finding Specific Sets of Documents
        5. Counting Documents
        6. Limiting Result Sets
          1. Limiting Results by Size
          2. Limiting Fields Returned in Objects
          3. Paging Results
        7. Sorting Result Sets
        8. Finding Distinct Field Values
        9. Grouping Results
        10. Applying MapReduce by Aggregating Results
          1. Understanding the aggregate() Method
          2. Using Aggregation Framework Operators
          3. Implementing Aggregation Expression Operators
          4. Aggregation Examples
        11. Summary
        12. Up Next
      6. 16. Using Mongoose for Structured Schema and Validation
        1. Understanding Mongoose
          1. Additional Objects
        2. Connecting to a MongoDB Database by Using Mongoose
        3. Defining a Schema
          1. Understanding Paths
          2. Creating a Schema Definition
          3. Adding Indexes to a Schema
          4. Implementing Unique Fields
          5. Forcing Required Fields
          6. Adding Methods to the Schema Object
          7. Implementing a Schema on the Words Database
        4. Compiling a Model
        5. Understanding the Query Object
          1. Setting Query Database Operation
          2. Setting the Query Database Operation Options
          3. Setting the Query Operators
        6. Understanding the Document Object
        7. Finding Documents by Using Mongoose
        8. Adding Documents by Using Mongoose
        9. Updating Documents by Using Mongoose
          1. Saving Document Changes
          2. Updating a Single Document
          3. Updating Multiple Documents
        10. Removing Documents by Using Mongoose
          1. Removing a Single Document
          2. Removing Multiple Documents
        11. Aggregating Documents by Using Mongoose
        12. Using the Validation Framework
        13. Implementing Middleware Functions
        14. Summary
        15. Up Next
      7. 17. Advanced MongoDB Concepts
        1. Adding Indexes
        2. Using Capped Collections
        3. Applying Replication
          1. Replication Strategy
          2. Deploying a Replica Set
        4. Implementing Sharding
          1. Sharding Server Types
          2. Choosing a Shard Key
          3. Selecting a Partitioning Method
          4. Deploying a Sharded MongoDB Cluster
        5. Implementing a GridFS Store
          1. Implementing a Grid Object from Node.js
          2. Implementing a GridStore Object from Node.js
        6. Repairing a MongoDB Database
        7. Backing Up MongoDB
        8. Summary
        9. Up Next
    14. IV: Using Express to Make Life Easier
      1. 18. Implementing Express in Node.js
        1. Getting Started with Express
          1. Configuring Express Settings
          2. Starting the Express Server
        2. Configuring Routes
          1. Implementing Routes
          2. Applying Parameters in Routes
        3. Using Request Objects
        4. Using Response Objects
          1. Setting Headers
          2. Setting the Status
          3. Sending Response
          4. Sending JSON Responses
          5. Sending Files
          6. Sending a Download Response
          7. Redirecting a Response
        5. Implementing a Template Engine
          1. Defining a Template Engine
          2. Adding Locals
          3. Creating Templates
          4. Rendering Templates in a Response
        6. Summary
        7. Up Next
      2. 19. Implementing Express Middleware
        1. Understanding Middleware
          1. Assigning Middleware Globally to a Path
          2. Assigning Middleware to a Single Route
          3. Adding Multiple Middleware Functions
        2. Using the query Middleware
        3. Serving Static Files
        4. Handling POST Body Data
        5. Sending and Receiving Cookies
        6. Implementing Sessions
        7. Applying Basic HTTP Authentication
        8. Implementing Session Authentication
        9. Creating Custom Middleware
        10. Summary
        11. Up Next
    15. V: Learning AngularJS
      1. 20. Getting Started with AngularJS
        1. Why AngularJS?
        2. Understanding AngularJS
          1. Modules
          2. Scopes and the Data Model
          3. Views with Templates and Directives
          4. Expressions
          5. Controllers
          6. Data Binding
          7. Services
          8. Dependency Injection
          9. Separation of Responsibilities
        3. An Overview of the AngularJS Life Cycle
          1. The Bootstrap Phase
          2. The Compilation Phase
          3. The Runtime Data Binding Phase
        4. Integrating AngularJS with Existing JavaScript and jQuery
        5. Adding AngularJS to the Node.js Environment
        6. Bootstrapping AngularJS in an HTML Document
        7. Using the Global APIs
        8. Creating a Basic AngularJS Application
          1. Loading the AngularJS Library and Your Main Module
          2. Defining the AngularJS Application Root Element
          3. Adding a Controller to the Template
          4. Implementing the Scope Model
        9. Summary
        10. Up Next
      2. 21. Understanding AngularJS Modules and Dependency Injection
        1. Overview of Modules and Dependency Injection
          1. Understanding Modules
          2. Dependency Injection
        2. Defining AngularJS Modules
          1. Creating an AngularJS Module Object
          2. Using Configuration Blocks
          3. Using Run Blocks
          4. Adding Providers to AngularJS Modules
        3. Implementing Dependency Injection
        4. Summary
        5. Up Next
      3. 22. Implementing the Scope as a Data Model
        1. Understanding Scopes
          1. The Relationship Between the Root Scope and Applications
          2. The Relationship Between Scopes and Controllers
          3. The Relationship Between Scopes and Templates
          4. The Relationship Between Scope and Backend Server Data
          5. The Scope Life Cycle
        2. Implementing Scope Hierarchy
        3. Emitting and Broadcasting Events
        4. Summary
        5. Up Next
      4. 23. Using AngularJS Templates to Create Views
        1. Understanding Templates
        2. Using Expressions
        3. Using Filters
          1. Using Built-in Filters
          2. Using Filters to Implement Ordering and Filtering
        4. Creating Custom Filters
        5. Summary
        6. Up Next
      5. 24. Implementing Directives in AngularJS Views
        1. Understanding Directives
        2. Using Built-in Directives
          1. Directives That Support AngularJS Functionality
          2. Directives That Extend Form Elements
          3. Directives That Bind the Model to Page Elements
          4. Directives That Bind Page Events to Controllers
        3. Creating Your Own Directives to Extend HTML
          1. Defining the Directive View Template
          2. Restricting Directive Behavior
          3. Replacing the Template Element
          4. Transcluding External Scopes
          5. Configuring the Directive Scope
          6. Manipulating the DOM with a Link Function
          7. Adding a Controller to a Directive
          8. Creating a Custom Directive
        4. Summary
        5. Up Next
      6. 25. Implementing AngularJS Services in Web Applications
        1. Understanding AngularJS Services
        2. Using the Built-in Services
          1. Sending HTTP GET and PUT Requests with the $http Service
          2. Using the $cacheFactory Service
          3. Implementing Browser Alerts Using the $window Service
          4. Interacting with Browser Cookies Using the $cookieStore Service
          5. Implementing Timers with $interval and $timeout Services
          6. Using the $animate Service
        3. Creating Custom Services
          1. Creating value Services
          2. Creating constant Services
          3. Creating factory Services
          4. Creating service Services
          5. Integrating Services in a Module
        4. Summary
        5. Up Next
    16. VI: Building Practical Web Application Components
      1. 26. Adding User Accounts to Your Website
        1. Libraries Used
        2. Project Directory Structure
        3. Defining the User Model
        4. Creating the Server
        5. Implementing Routes
        6. Implementing the User Controller Routes
          1. Implementing the User Signup Route
          2. Implementing the User Login Route
          3. Implementing the Get User Profile Route
          4. Implementing the Update User Route
          5. Implementing the Delete User Route
          6. Full User Controller Code
        7. Implementing the User and Authentication Views
          1. Implementing the signup View
          2. Implementing the index View
          3. Implementing the login View
          4. Implementing the user View
          5. Adding CSS Code to Style Views
        8. Implementing the AngularJS Module and Controller
        9. Using Social Media Accounts as Authentication Sources
          1. Adding Passport
          2. Adding Authentication Strategies
          3. Implementing Application Middleware
          4. Adding Authenticated User Serialization and Deserialization
          5. Implementing Routes to Use Authentication
          6. Authenticating Using Google
        10. Summary
        11. Up Next
      2. 27. Adding Comment Threads to Pages
        1. Libraries Used
        2. Project Directory Structure
        3. Defining the Comment, Reply, Photo, and Page Models
          1. Defining the Page Model
          2. Defining the Photo Model
          3. Defining the CommentThread Model
        4. Creating the Comments Server
        5. Implementing Routes to Support Viewing and Adding Comments
        6. Implementing the Model-Based Controller Routes
          1. Implementing the Page Model Controller
          2. Implementing the Photo Model Controller
          3. Implementing the CommentThread Model Controller
        7. Implementing Photo and Comment Views
          1. Implementing the Photo View
          2. Implementing the Comment Thread View
          3. Implementing the Comment View
          4. Adding CSS to Stylize the Views
        8. Implementing the AngularJS Module and Controller to Support Comment Views
          1. Creating a Comment Service
          2. Implementing the Photo Controller
          3. Implementing the Page AngularJS Controller
          4. The Full AngularJS Application
        9. Initializing the Application
        10. Summary
        11. Up Next
      3. 28. Creating Your Own Shopping Cart
        1. Project Description
        2. Libraries Used
        3. Project Directory Structure
        4. Defining the Customer, Product, and Orders Models
          1. Defining the Address Schema
          2. Defining the Billing Schema
          3. Defining the Product Schema
          4. Defining the Quantity Schema
          5. Defining the Order Schema
          6. Defining the Customer Schema
        5. Creating the Shopping Cart Server
        6. Implementing Routes to Support Product, Cart, and Order Requests
        7. Implementing the Model-Based Controller Routes
          1. Implementing the Product Model Controller
          2. Implementing the Order Model Controller
          3. Implementing the Customer Model Controller
        8. Implementing Shopping Cart and Checkout Views
          1. Implementing the Shopping View
          2. Implementing the Products View
          3. Implementing the Product Page View
          4. Implementing the Cart View
          5. Implementing the Shipping View
          6. Implementing the Billing View
          7. Implementing the Review View
          8. Implementing the Orders View
          9. Adding CSS to Stylize the Views
        9. Implementing the AngularJS Module and Controller to Support Shopping Cart Views
          1. Initializing the Shopping Scope
          2. Implementing Helper Functions
          3. Adding Items to the Cart
          4. Deleting Items from the Cart
          5. Checking Out
          6. Setting Shipping Information
          7. Verifying Billing
          8. Making the Purchase
          9. The Full Controller
        10. Initializing the Application
        11. Summary
        12. Up Next
      4. 29. Building Interactive Web 2.0 Application Components
        1. Project Description
        2. Libraries Used
        3. Project Directory Structure
        4. Defining the Project Model
        5. Creating the Application Server
        6. Implementing Routes to Support the Views
        7. Implementing a Tabbed View
          1. Creating the AngularJS Tabbed View Templates
          2. Defining the Pane Template
          3. Defining the Tabs Template
          4. Implementing the Tabbed View
          5. Implementing the AngularJS Custom Directives
          6. Styling the Tabs View
        8. Implementing a Weather Service View
          1. Creating the Backend Weather Service
          2. Defining the Weather AngularJS Controller
          3. Defining the Weather AngularJS View
          4. Styling the Weather View
        9. Implementing Draggable Elements
          1. Defining the Draggable Custom AngularJS Directive
          2. Implementing the Draggable Directive in an AngularJS View
          3. Styling the Draggable View
        10. Implementing Dynamic Data Access
          1. Creating the Express Route Controller for the /words Route
          2. Defining the Table AngularJS Controller
          3. Implementing the Tables AngularJS View
          4. Styling the Tables View
        11. Initializing the Application
        12. Summary
    17. Index