Web Development with Node and Express

Book description

Learn how to build dynamic web applications with Express, a key component of the Node/JavaScript development stack. In this hands-on guide, author Ethan Brown teaches you the fundamentals through the development of a fictional application that exposes a public website and a RESTful API. You’ll also learn web architecture best practices to help you build single-page, multi-page, and hybrid web apps with Express.

Express strikes a balance between a robust framework and no framework at all, allowing you a free hand in your architecture choices. With this book, frontend and backend engineers familiar with JavaScript will discover new ways of looking at web development.

  • Create webpage templating system for rendering dynamic data
  • Dive into request and response objects, middleware, and URL routing
  • Simulate a production environment for testing and development
  • Focus on persistence with document databases, particularly MongoDB
  • Make your resources available to other programs with RESTful APIs
  • Build secure apps with authentication, authorization, and HTTPS
  • Integrate with social media, geolocation, and other third-party services
  • Implement a plan for launching and maintaining your app
  • Learn critical debugging skills

    This book covers Express 4.0.

Publisher resources

View/Submit Errata

Table of contents

  1. Dedication
  2. Foreword
  3. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
    3. Example Website
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  4. 1. Introducing Express
    1. The JavaScript Revolution
    2. Introducing Express
    3. A Brief History of Express
    4. Upgrading to Express 4.0
    5. Node: A New Kind of Web Server
    6. The Node Ecosystem
    7. Licensing
  5. 2. Getting Started with Node
    1. Getting Node
    2. Using the Terminal
    3. Editors
    4. npm
    5. A Simple Web Server with Node
      1. Hello World
      2. Event-Driven Programming
      3. Routing
      4. Serving Static Resources
    6. Onward to Express
  6. 3. Saving Time with Express
    1. Scaffolding
    2. The Meadowlark Travel Website
    3. Initial Steps
      1. Views and Layouts
      2. Static Files and Views
      3. Dynamic Content in Views
      4. Conclusion
  7. 4. Tidying Up
    1. Best Practices
    2. Version Control
    3. How to Use Git with This Book
      1. If You’re Following Along by Doing It Yourself
      2. If You’re Following Along by Using the Official Repository
    4. npm Packages
    5. Project Metadata
    6. Node Modules
  8. 5. Quality Assurance
    1. QA: Is It Worth It?
    2. Logic Versus Presentation
    3. The Types of Tests
    4. Overview of QA Techniques
    5. Running Your Server
    6. Page Testing
    7. Cross-Page Testing
    8. Logic Testing
    9. Linting
    10. Link Checking
    11. Automating with Grunt
    12. Continuous Integration (CI)
  9. 6. The Request and Response Objects
    1. The Parts of a URL
    2. HTTP Request Methods
    3. Request Headers
    4. Response Headers
    5. Internet Media Types
    6. Request Body
    7. Parameters
    8. The Request Object
    9. The Response Object
    10. Getting More Information
    11. Boiling It Down
      1. Rendering Content
      2. Processing Forms
      3. Providing an API
  10. 7. Templating with Handlebars
    1. There Are No Absolute Rules Except This One
    2. Choosing a Template Engine
    3. Jade: A Different Approach
    4. Handlebars Basics
      1. Comments
      2. Blocks
      3. Server-Side Templates
      4. Views and Layouts
      5. Using Layouts (or Not) in Express
      6. Partials
      7. Sections
      8. Perfecting Your Templates
      9. Client-Side Handlebars
    5. Conclusion
  11. 8. Form Handling
    1. Sending Client Data to the Server
    2. HTML Forms
    3. Encoding
    4. Different Approaches to Form Handling
    5. Form Handling with Express
    6. Handling AJAX Forms
    7. File Uploads
    8. jQuery File Upload
  12. 9. Cookies and Sessions
    1. Externalizing Credentials
    2. Cookies in Express
    3. Examining Cookies
    4. Sessions
      1. Memory Stores
      2. Using Sessions
    5. Using Sessions to Implement Flash Messages
    6. What to Use Sessions For
  13. 10. Middleware
    1. Common Middleware
    2. Third-Party Middleware
  14. 11. Sending Email
    1. SMTP, MSAs, and MTAs
    2. Receiving Email
    3. Email Headers
    4. Email Formats
    5. HTML Email
    6. Nodemailer
      1. Sending Mail
      2. Sending Mail to Multiple Recipients
    7. Better Options for Bulk Email
    8. Sending HTML Email
      1. Images in HTML Email
      2. Using Views to Send HTML Email
      3. Encapsulating Email Functionality
    9. Email as a Site Monitoring Tool
  15. 12. Production Concerns
    1. Execution Environments
    2. Environment-Specific Configuration
    3. Scaling Your Website
      1. Scaling Out with App Clusters
      2. Handling Uncaught Exceptions
      3. Scaling Out with Multiple Servers
    4. Monitoring Your Website
      1. Third-Party Uptime Monitors
      2. Application Failures
    5. Stress Testing
  16. 13. Persistence
    1. Filesystem Persistence
    2. Cloud Persistence
    3. Database Persistence
      1. A Note on Performance
      2. Setting Up MongoDB
      3. Mongoose
      4. Database Connections with Mongoose
      5. Creating Schemas and Models
      6. Seeding Initial Data
      7. Retrieving Data
      8. Adding Data
      9. Using MongoDB for Session Storage
  17. 14. Routing
    1. Routes and SEO
    2. Subdomains
    3. Route Handlers Are Middleware
    4. Route Paths and Regular Expressions
    5. Route Parameters
    6. Organizing Routes
    7. Declaring Routes in a Module
    8. Grouping Handlers Logically
    9. Automatically Rendering Views
    10. Other Approaches to Route Organization
  18. 15. REST APIs and JSON
    1. JSON and XML
    2. Our API
    3. API Error Reporting
    4. Cross-Origin Resource Sharing (CORS)
    5. Our Data Store
    6. Our Tests
    7. Using Express to Provide an API
    8. Using a REST Plugin
    9. Using a Subdomain
  19. 16. Static Content
    1. Performance Considerations
    2. Future-Proofing Your Website
      1. Static Mapping
      2. Static Resources in Views
      3. Static Resources in CSS
    3. Static Resources in Server-Side JavaScript
    4. Static Resources in Client-Side JavaScript
    5. Serving Static Resources
    6. Changing Your Static Content
    7. Bundling and Minification
      1. Skipping Bundling and Minification in Development Mode
    8. A Note on Third-Party Libraries
    9. QA
    10. Summary
  20. 17. Implementing MVC in Express
    1. Models
    2. View Models
    3. Controllers
    4. Conclusion
  21. 18. Security
    1. HTTPS
      1. Generating Your Own Certificate
      2. Using a Free Certificate Authority
      3. Purchasing a Certificate
      4. Enabling HTTPS for Your Express App
      5. A Note on Ports
      6. HTTPS and Proxies
    2. Cross-Site Request Forgery
    3. Authentication
      1. Authentication Versus Authorization
      2. The Problem with Passwords
      3. Third-Party Authentication
      4. Storing Users in Your Database
      5. Authentication Versus Registration and the User Experience
      6. Passport
        1. Setting up Passport
      7. Role-Based Authorization
      8. Adding Additional Authentication Providers
    4. Conclusion
  22. 19. Integrating with Third-Party APIs
    1. Social Media
      1. Social Media Plugins and Site Performance
      2. Searching for Tweets
      3. Rendering Tweets
    2. Geocoding
      1. Geocoding with Google
        1. Usage restrictions
      2. Geocoding Your Data
      3. Displaying a Map
      4. Improving Client-Side Performance
    3. Weather Data
    4. Conclusion
  23. 20. Debugging
    1. The First Principle of Debugging
    2. Take Advantage of REPL and the Console
    3. Using Node’s Built-in Debugger
    4. Node Inspector
    5. Debugging Asynchronous Functions
    6. Debugging Express
  24. 21. Going Live
    1. Domain Registration and Hosting
      1. Domain Name System
      2. Security
      3. Top-Level Domains
      4. Subdomains
      5. Nameservers
      6. Hosting
        1. Traditional hosting, or cloud hosting?
        2. XaaS
        3. The behemoths
        4. Boutique hosting
      7. Deployment
        1. Git deployment
        2. Deployment to Azure
        3. Manual Git-based deployment
        4. Amazon deployment with Elastic Beanstalk
    2. Conclusion
  25. 22. Maintenance
    1. The Principles of Maintenance
      1. Have a Longevity Plan
      2. Use Source Control
      3. Use an Issue Tracker
      4. Exercise Good Hygiene
      5. Don’t Procrastinate
      6. Do Routine QA Checks
      7. Monitor Analytics
      8. Optimize Performance
      9. Prioritize Lead Tracking
      10. Prevent “Invisible” Failures
    2. Code Reuse and Refactoring
      1. Private npm Registry
      2. Middleware
        1. Module exposes middleware function directly
        2. Module exposes a function that returns middleware
        3. Module exposes an object that contains middleware
        4. Module exposes an object constructor
    3. Conclusion
  26. 23. Additional Resources
    1. Online Documentation
    2. Periodicals
    3. Stack Overflow
    4. Contributing to Express
    5. Conclusion
  27. Index
  28. Colophon
  29. Copyright

Product information

  • Title: Web Development with Node and Express
  • Author(s): Ethan Brown
  • Release date: July 2014
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491949306