Book Description

While there have been quite a few attempts to get JavaScript working as a server-side language, Node.js (frequently just called Node) has been the first environment that's gained any traction. It's now used by companies such as Netflix, Uber and Paypal to power their web apps. Node allows for blazingly fast performance; thanks to its event loop model, common tasks like network connection and database I/O can be executed very quickly indeed.

In this book, we offer a selection of nine different practical projects that you can follow along with.

It contains:

  • Build a Simple Beginner App with Node, Bootstrap & MongoDB by James Hibbard
  • How to Build a File Upload Form with Express and Dropzone.js by Lukas White
  • How to Build and Structure a Node.js MVC Application by James Kolce
  • User Authentication with the MEAN Stack by Simon Holmes & Jeremy Wilken
  • Build a JavaScript Command Line Interface (CLI) with Node.js by Lukas White & Michael Wanyoike
  • Building a Real-time Chat App with Sails.js by Michael Wanyoike
  • Passport Authentication for Node.js Applications by Paul Orac
  • Local Authentication Using Passport in Node.js by Paul Orac
  • An Introduction to NodeBots by Patrick Catanzariti

This book is for anyone who wants to start learning server-side development with Node.js. Familiarity with JavaScript is assumed.

Table of Contents

  1. 9 Practical Node.js Projects
  2. Notice of Rights
  3. Notice of Liability
  4. Trademark Notice
  5. About SitePoint
  6. Preface
    1. Who Should Read This Book?
    2. Conventions Used
      1. Code Samples
      2. Tips, Notes, and Warnings
  7. Chapter 1: Build a Simple Beginner App with Node, Bootstrap & MongoDB
    1. by James Hibbard
    2. What We’ll Be Building
    3. Basic Setup
      1. Node.js
      2. npm
      3. MongoDB
      4. A MongoDB GUI
      5. Check that Everything Is Installed Correctly
      6. Check the Database Connection Using Compass
    4. Initialize the Application
      1. Install Express
      2. Install nodemon
      3. Create Some Initial Files
      4. Kick off the App
    5. Basic Templating with Pug
      1. Install Pug and Integrate It into the Express App
      2. Alter the Route to Use Our Template
      3. Define a Layout File for Pug
      4. Use the Layout File from the Child Template
    6. Dealing with Forms in Express
      1. Handle Form Input
      2. A Note about Request and Response Objects
      3. Validating Form Input
    7. Interact with a Database
      1. Specify Connection Details
      2. Connect to the Database
      3. Define a Mongoose Schema
      4. Save Data to the Database
      5. Retrieve Data from the Database
    8. Add HTTP Authentication
    9. Serve Static Assets in Express
      1. Style the App with Bootstrap
    10. Conclusion
  8. Chapter 2: How to Build a File Upload Form with Express and Dropzone.js
    1. by Lukas White
    2. Introducing DropzoneJS
      1. Features
      2. Browser Support
    3. Installation
    4. First Steps
    5. Basic Usage
    6. Basic Configuration Options
      1. Enforcing Maximum File Size
      2. Restricting to Certain File Types
      3. Modifying the Size of the Thumbnail
      4. Additional File Checks
    7. Sending Additional Headers
    8. Handling Fallbacks
    9. Handling Errors
    10. Overriding Messages and Translation
    11. Events
    12. A More Complex Validation Example: Image Dimensions
    13. A Complete Example
    14. Theming
    15. A Simple Server-Side Upload Handler with Node.js and Express
    16. Handling Upload Success
    17. Handling Upload Errors
    18. Summary
  9. Chapter 3: How to Build and Structure a Node.js MVC Application
    1. by James Kolce
    2. What is MVC?
    3. Laying out the Foundation
    4. Storing Our Settings
    5. Defining the Routes
    6. Building the Models
      1. Setting up the database
      2. Creating our Note model
      3. Synchronizing the Database
    7. Building the controllers
      1. The Home Controller
      2. Boilerplate of the Note Controller
      3. The “create” function
      4. The “read” function
      5. The “update” function
      6. The “delete” function
      7. Using the Note controller in our routes
    8. Building the Views
      1. The note component
      2. The base layout
      3. The home view
      4. The note view
      5. The JavaScript on the client
      6. Adding support for views on the server
      7. Setting the home view
      8. Setting the note view: create function
      9. Setting the note view: read function
      10. Setting the note view: update function
    9. Serving Static Files
    10. Conclusion
  10. Chapter 4: User Authentication with the MEAN Stack
    1. by Simon Holmes & Jeremy Wilken
    2. The MEAN Stack Authentication Flow
    3. The Example Application
      1. The Angular App
      2. The REST API
    4. Creating the MongoDB Data Schema with Mongoose
      1. Managing the Password without Saving It
      2. Generating a JSON Web Token (JWT)
    5. Set Up Passport to Handle the Express Authentication
      1. Configure Passport
    6. Configure API Endpoints
      1. Code the Register and Login API Controllers
      2. Securing an API Route
    7. Create Angular Authentication Service
      1. Local Storage: Saving, Reading and Deleting a JWT
      2. Check Whether a User Is Logged In
      3. Structuring the API Calls
      4. Calling the Register and Login API Endpoints
    8. Apply Authentication to Angular App
      1. Connect the Register and Login Controllers
      2. Change Content Based on User Status
      3. Protect a Route for Logged in Users Only
      4. Call a Protected API Route
  11. Chapter 5: Build a JavaScript Command Line Interface (CLI) with Node.js
    1. by Lukas White & Michael Wanyoike
    2. Why Build a Command-line Tool with Node.js?
    3. What We’re Going to Build: ginit
    4. The Application Dependencies
    5. Getting Started
    6. Adding Some Helper Methods
    7. Initializing the Node CLI
    8. Prompting the User for Input
    9. Dealing With GitHub Authentication
      1. Storing Config
    10. Communicating with the GitHub API
    11. Creating a Repository
    12. Creating a .gitignore File
    13. Interacting with Git from Within the App
    14. Putting It All Together
    15. Making the ginit Command Available Globally
    16. Taking it Further
  12. Chapter 6: Building a Real-time Chat App with Sails.js
    1. by Michael Wanyoike
    2. Prerequisites
    3. Project Plan
    4. Installing Sails.js
    5. Installing Front-end Dependencies
      1. 1. Manual Download
      2. 2. Using Bower
      3. 3. Using npm + grunt.copy
      4. Testing Dependencies Installation
    6. Creating Views
      1. Home Page Design
      2. Navigation Menu
      3. Profile
      4. Chat Room Layout
    7. Routing
    8. Generating a User API
      1. Profile Form
    9. ChatMessage API
      1. Chat Messages UI
      2. Chat Post UI
    10. Basic Authentication
      1. Login/Sign Up Form
      2. Policy
      3. Auth Controller and Service
    11. Final Touches
    12. Summary
  13. Chapter 7: Passport Authentication for Node.js Applications
    1. by Paul Orac
    2. Express Web App
    3. Setting up Passport
    4. Implementing Facebook Authentication
    5. Implementing GitHub Authentication
    6. Conclusion
  14. Chapter 8: Local Authentication Using Passport in Node.js
    1. by Paul Orac
    2. Prerequisites
    3. Creating the Project
    4. HTML
    5. Setting up Express
    6. Setting up Passport
    7. Creating a MongoDB Data Store
      1. Retrieving Stored Data
    8. Connection Mongo to Node with Mongoose
    9. Implementing Local Authentication
    10. Conclusion
  15. Chapter 9: An Introduction to NodeBots
    1. by Patrick Catanzariti
    2. What is a Microcontroller?
    3. What are NodeBots?
    4. Where To Start
      1. Starter Kits
    5. Resources for Learning NodeBots
      1. The SimpleBot
    6. Conclusion