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 Angular Web Development, 2nd Edition

Book Description

Node.js, MongoDB and Angular Web Development

The definitive guide to using the MEAN stack to build web applications

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

Updated for Angular 2, Angular 4, and subsequent versions, this new edition of Node.js, MongoDB and Angular Web Development shows you how to integrate these three technologies into complete working solutions. It begins with concise, crystal-clear tutorials on each technology and then quickly moves on to building common web applications.

You’ll learn how to use Node.js and MongoDB to build more scalable, high-performance sites, how to leverage Angular’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
  • Implement a MongoDB data store for your web applications
  • Access and interact with MongoDB from Node.js JavaScript code
  • Learn the basics of TypeScript
  • Define custom Angular directives that extend the HTML language
  • Build server-side web services in JavaScript
  • 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 and nested comment components to your web applications and pages

Contents at a Glance

Part I: Getting Started

1 Introducing the Node.js-to-Angular Stack

2 JavaScript Primer

Part II: Learning Node.js

3 Getting Started with Node.js

4 Using Events, Listeners, Timers, and Callbacks in Node.js

5 Handling Data I/O in Node.js

6 Accessing the File System from Node.js

7 Implementing HTTP Services in Node.js

8 Implementing Socket Services in Node.js

9 Scaling Applications Using Multiple Processors in Node.js

10 Using Additional Node.js Modules

Part III: Learning MongoDB

11 Understanding NoSQL and MongoDB

12 Getting Started with MongoDB

13 Getting Started with MongoDB and Node.js

14 Manipulating MongoDB Documents from Node.js

15 Accessing MongoDB from Node.js

16 Using Mongoose for Structured Schema and Validation

17 Advanced MongoDB Concepts

Part IV: Using Express to Make Life Easier

18 Implementing Express in Node.js

19 Implementing Express Middleware

Part V: Learning Angular

20 Jumping into TypeScript

21 Getting Started with Angular

22 Angular Components

23 Expressions

24 Data Binding

25 Built-in Directives

Part VI: Advanced Angular

26 Custom Directives

27 Events and Change Detection

28 Implementing Angular Services in Web Applications

29 Creating Your Own Custom Angular Services

30 Having Fun with Angular

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Contents At a Glance
  5. Contents
  6. About the Author
  7. Acknowledgments
  8. Accessing the Free Web Edition
  9. Introduction
  10. I: Getting Started
    1. 1 Introducing the Node.js-to-Angular Stack
      1. Understanding the Basic Web Development Framework
        1. User
        2. Browser
        3. Webserver
        4. Backend Services
      2. Understanding the Node.js-to-Angular Stack Components
        1. Node.js
        2. MongoDB
        3. Express
        4. Angular
      3. Summary
      4. Next
    2. 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 and Removing Items to Arrays
      10. Adding Error Handling
        1. try/catch Blocks
        2. Throw Your Own Errors
        3. Using finally
      11. Summary
      12. Next
  11. 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?
        3. What Does Node.js Come With?
      2. Installing Node.js
        1. Looking at the Node.js Install Location
        2. Verify Node.js Executables
        3. Selecting a Node.js IDE
      3. Working with Node Packages
        1. What Are Node Packaged Modules?
        2. Understanding the Node Package Registry
        3. Using the Node Package Manager
        4. Searching for Node Package 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. 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. The 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. 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. 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 Write
        2. Synchronous File Writing
        3. Asynchronous File Writing
        4. Streaming File Writing
      4. Reading Files
        1. Simple File Read
        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. 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. Next
    6. 8 Implementing Socket Services in Node.js
      1. Understanding Network Sockets
      2. Understanding TPC 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. 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 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. 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. Converting JavaScript Objects to Strings
        4. Inheriting Functionality from Other Objects
      3. Using the dns Module
      4. Using the crypto Module
      5. Other Node Modules and Objects
      6. Summary
      7. Next
  12. 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. 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 in a Collection
        7. Updating Documents in a Collection
      6. Summary
      7. 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. Connecting to MongoDB from Node.js Using the MongoClient Object
      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. 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. Upserting Documents in Collection
      9. Deleting Documents from a Collection
      10. Removing a Single Document from a Collection
      11. Summary
      12. Next
    5. 15 Accessing MongoDB 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. Next
    6. 16 Using Mongoose for Structured Schema and Validation
      1. Understanding Mongoose
        1. Additional Objects
      2. Connecting to a MongoDB Database 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 Model
        7. Implementing the Schema on the Words Database
      4. Compiling a Model
      5. Understanding the Query Object
        1. Setting the Query Database Operation
        2. Setting the Query Database Operation Options
        3. Setting the Query Operators
      6. Understanding the Document Object
      7. Finding Documents Using Mongoose
      8. Adding Documents Using Mongoose
      9. Updating Documents Using Mongoose
        1. Saving Document Changes
        2. Updating a Single Document
        3. Updating Multiple Documents
      10. Removing Documents Using Mongoose
        1. Removing a Single Document
        2. Removing Multiple Documents
      11. Aggregating Documents Using Mongoose
      12. Using the Validation Framework
      13. Implementing Middleware Functions
      14. Summary
      15. 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. Repairing a MongoDB Database
      6. Backing Up MongoDB
      7. Summary
      8. Next
  13. 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 Requests 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 the Response
      5. Implementing a Template Engine
        1. Defining the Engine
        2. Adding Locals
        3. Creating Templates
        4. Rendering Templates in a Response
      6. Summary
      7. 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. Next
  14. V: Learning Angular
    1. 20 Jumping into TypeScript
      1. Learning the Different Types
      2. Understanding Interfaces
      3. Implementing Classes
        1. Class Inheritance
      4. Implementing Modules
      5. Understanding Functions
      6. Summary
      7. Next
    2. 21 Getting Started with Angular
      1. Why Angular?
      2. Understanding Angular
        1. Modules
        2. Directives
        3. Data Binding
        4. Dependency Injection
        5. Services
      3. Separation of Responsibilities
      4. Adding Angular to Your Environment
      5. Using the Angular CLI
        1. Generating Content with the CLI
      6. Creating a Basic Angular Application
        1. Creating Your First Angular App
        2. Understanding and Using NgModule
        3. Creating the Angular Bootstrapper
      7. Summary
      8. Next
    3. 22 Angular Components
      1. Component Configuration
        1. Defining a Selector
      2. Building a Template
        1. Using Inline CSS and HTML in Angular Applications
      3. Using Constructors
      4. Using External Templates
      5. Injecting Directives
        1. Building a Nested Component with Dependency Injection
        2. Passing in Data with Dependency Injection
        3. Creating an Angular Application that Uses Inputs
      6. Summary
      7. Next
    4. 23 Expressions
      1. Using Expressions
        1. Using Basic Expressions
        2. Interacting with the Component Class in Expressions
        3. Using TypeScript in Angular Expressions
      2. Using Pipes
        1. Using Built-in Pipes
      3. Building a Custom Pipe
        1. Creating a Custom Pipe
      4. Summary
      5. Next
    5. 24 Data Binding
      1. Understanding Data Binding
        1. Interpolation
        2. Property Binding
        3. Attribute Binding
        4. Class Binding
        5. Style Binding
        6. Event Binding
        7. Two-Way Binding
      2. Summary
      3. Next
    6. 25 Built-in Directives
      1. Understanding Directives
      2. Using Built-in Directives
        1. Components Directives
        2. Structural Directives
        3. Attribute Directives
      3. Summary
      4. Next
  15. VI: Advanced Angular
    1. 26 Custom Directives
      1. Creating a Custom Attribute Directive
      2. Creating a Custom Directive with a Component
      3. Summary
      4. Next
    2. 27 Events and Change Detection
      1. Using Browser Events
      2. Emitting Custom Events
        1. Emitting a Custom Event to the Parent Component Hierarchy
        2. Handling Custom Events with a Listener
        3. Implementing Custom Events in Nested Components
        4. Deleting Data in a Parent Component from a Child Component
      3. Using Observables
        1. Creating an Observable Object
        2. Watching for Data Changes with Observables
      4. Summary
      5. Next
    3. 28 Implementing Angular Services in Web Applications
      1. Understanding Angular Services
      2. Using the Built-in Services
      3. Sending HTTP GET and PUT Requests with the http Service
        1. Configuring the HTTP Request
        2. Implementing the HTTP Response Callback Functions
        3. Implementing a Simple JSON File and Using the http Service to Access It
      4. Implementing a Simple Mock Server Using the http Service
        1. Implementing a Simple Mock Server and Using the http Service to Update Items on the Server
      5. Changing Views with the router Service
        1. Using routes in Angular
        2. Implementing a Simple Router
      6. Implementing a Router with a Navigation Bar
      7. Implementing a Router with Parameters
      8. Summary
      9. Next
    4. 29 Creating Your Own Custom Angular Services
      1. Integrating Custom Services into Angular Applications
        1. Adding an Angular Service to an Application
      2. Implementing a Simple Application that Uses a Constant Data Service
      3. Implementing a Data Transform Service
      4. Implementing a Variable Data Service
      5. Implementing a Service that Returns a Promise
      6. Implementing a Shared Service
      7. Summary
      8. Next
    5. 30 Having Fun with Angular
      1. Implementing an Angular Application that Uses the Animation Service
      2. Implementing an Angular Application that Zooms in on Images
      3. Implementing an Angular Application that Enables Drag and Drop
      4. Implementing a Star Rating Angular Component
      5. Summary
  16. Index