Web Development with Node and Express, 2nd Edition

Book description

Build dynamic web applications with Express, a key component of the Node/JavaScript development stack. In this updated edition, author Ethan Brown teaches you Express fundamentals by walking you through the development of an example application. This hands-on guide covers everything from server-side rendering to API development suitable for usein single-page apps (SPAs).

Express strikes a balance between a robust framework and no framework at all, allowing you a free hand in your architecture choices. Frontend and backend engineers familiar with JavaScript will also learn best practices for building multipage and hybrid web apps with Express. Pick up this book anddiscover new ways to look at web development.

  • Create a templating system for rendering dynamic data
  • Dive into request and response objects, middleware, and URL routing
  • Simulate a production environment for testing
  • Persist data in document databases with MongoDB and relational databases with PostgreSQL
  • Make your resources available to other programs with APIs
  • Build secure apps with authentication, authorization, and HTTPS
  • Integrate with social media, geolocation, and more
  • Implement a plan for launching and maintaining your app
  • Learn critical debugging skills

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who This Book Is For
    2. Notes on the Second Edition
    3. How This Book Is Organized
    4. Example Website
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. 1. Introducing Express
    1. The JavaScript Revolution
    2. Introducing Express
    3. Server-Side and Client-Side Applications
    4. A Brief History of Express
    5. Node: A New Kind of Web Server
    6. The Node Ecosystem
    7. Licensing
    8. Conclusion
  3. 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
  4. 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
  5. 4. Tidying Up
    1. File and Directory Structure
    2. Best Practices
    3. Version Control
    4. 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
    5. npm Packages
    6. Project Metadata
    7. Node Modules
    8. Conclusion
  6. 5. Quality Assurance
    1. The QA Plan
    2. QA: Is It Worth It?
    3. Logic Versus Presentation
    4. The Types of Tests
    5. Overview of QA Techniques
    6. Installing and Configuring Jest
    7. Unit Testing
      1. Mocking
      2. Refactoring the Application for Testability
      3. Writing Our First Test
      4. Test Maintenance
      5. Code Coverage
    8. Integration Testing
    9. Linting
    10. Continuous Integration
    11. Conclusion
  7. 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. The Request Object
    8. The Response Object
    9. Getting More Information
    10. Boiling It Down
      1. Rendering Content
      2. Processing Forms
      3. Providing an API
    11. Conclusion
  8. 7. Templating with Handlebars
    1. There Are No Absolute Rules Except This One
    2. Choosing a Template Engine
    3. Pug: 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. Sections
      7. Partials
      8. Perfecting Your Templates
    5. Conclusion
  9. 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. Using Fetch to Send Form Data
    7. File Uploads
      1. File Uploads with Fetch
    8. Improving File Upload UI
    9. Conclusion
  10. 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
    7. Conclusion
  11. 10. Middleware
    1. Middleware Principles
    2. Middleware Examples
    3. Common Middleware
    4. Third-Party Middleware
    5. Conclusion
  12. 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. Conclusion
  13. 12. Production Concerns
    1. Execution Environments
    2. Environment-Specific Configuration
    3. Running Your Node Process
    4. Scaling Your Website
      1. Scaling Out with App Clusters
      2. Handling Uncaught Exceptions
      3. Scaling Out with Multiple Servers
    5. Monitoring Your Website
      1. Third-Party Uptime Monitors
    6. Stress Testing
    7. Conclusion
  14. 13. Persistence
    1. Filesystem Persistence
    2. Cloud Persistence
    3. Database Persistence
      1. A Note on Performance
      2. Abstracting the Database Layer
      3. Setting Up MongoDB
      4. Mongoose
      5. Database Connections with Mongoose
      6. Creating Schemas and Models
      7. Seeding Initial Data
      8. Retrieving Data
      9. Adding Data
      10. PostgreSQL
      11. Adding Data
    4. Using a Database for Session Storage
    5. Conclusion
  15. 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. Conclusion
  16. 15. REST APIs and JSON
    1. JSON and XML
    2. Our API
    3. API Error Reporting
    4. Cross-Origin Resource Sharing
    5. Our Tests
    6. Using Express to Provide an API
    7. Conclusion
  17. 16. Single-Page Applications
    1. A Short History of Web Application Development
    2. SPA Technologies
    3. Creating a React App
    4. React Basics
      1. The Home Page
      2. Routing
      3. Vacations Page—Visual Design
      4. Vacations Page—Server Integration
      5. Sending Information to the Server
      6. State Management
      7. Deployment Options
    5. Conclusion
  18. 17. Static Content
    1. Performance Considerations
    2. Content Delivery Networks
    3. Designing for CDNs
      1. Server-Rendered Website
      2. Single-Page Applications
    4. Caching Static Assets
    5. Changing Your Static Content
    6. Conclusion
  19. 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
      7. Role-Based Authorization
      8. Adding Authentication Providers
    4. Conclusion
  20. 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
      2. Geocoding Your Data
      3. Displaying a Map
    3. Weather Data
    4. Conclusion
  21. 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 Clients
    5. Debugging Asynchronous Functions
    6. Debugging Express
    7. Conclusion
  22. 21. Going Live
    1. Domain Registration and Hosting
      1. Domain Name System
      2. Security
      3. Top-Level Domains
      4. Subdomains
      5. Nameservers
      6. Hosting
      7. Deployment
    2. Conclusion
  23. 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
    3. Conclusion
  24. 23. Additional Resources
    1. Online Documentation
    2. Periodicals
    3. Stack Overflow
    4. Contributing to Express
    5. Conclusion
  25. Index

Product information

  • Title: Web Development with Node and Express, 2nd Edition
  • Author(s): Ethan Brown
  • Release date: November 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492053514