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
Author(s): Ethan Brown
Edition: 2
Publisher: O'Reilly Media
Year: 2019
Language: English
Pages: 346
Tags: Web Development; Node; Express; JavaScript;
Cover
Web Development with Node and Express
Table of Contents
Preface
1
Introducing Express
The JavaScript Revolution
Introducing Express
Server-Side and Client-Side Applications
A Brief History of Express
Node: A New Kind of Web Server
The Node Ecosystem
Licensing
Conclusion
2
Getting Started with Node
Getting Node
Using the Terminal
Editors
npm
A Simple Web Server with Node
Hello World
Event-Driven Programming
Routing
Serving Static Resources
Onward to Express
3
Saving Time with Express
Scaffolding
The Meadowlark Travel Website
Initial Steps
Views and Layouts
Static Files and Views
Dynamic Content in Views
Conclusion
4
Tidying Up
File and Directory Structure
Best Practices
Version Control
How to Use Git with This Book
If You’re Following Along by Doing It Yourself
If You’re Following Along by Using the Official Repository
npm Packages
Project Metadata
Node Modules
Conclusion
5
Quality Assurance
The QA Plan
QA: Is It Worth It?
Logic Versus Presentation
The Types of Tests
Overview of QA Techniques
Installing and Configuring Jest
Unit Testing
Mocking
Refactoring the Application for Testability
Writing Our First Test
Test Maintenance
Code Coverage
Integration Testing
Linting
Continuous Integration
Conclusion
6
The Request and Response Objects
The Parts of a URL
HTTP Request Methods
Request Headers
Response Headers
Internet Media Types
Request Body
The Request Object
The Response Object
Getting More Information
Boiling It Down
Rendering Content
Processing Forms
Providing an API
Conclusion
7
Templating with Handlebars
There Are No Absolute Rules Except This One
Choosing a Template Engine
Pug: A Different Approach
Handlebars Basics
Comments
Blocks
Server-Side Templates
Views and Layouts
Using Layouts (or Not) in Express
Sections
Partials
Perfecting Your Templates
Conclusion
8
Form Handling
Sending Client Data to the Server
HTML Forms
Encoding
Different Approaches to Form Handling
Form Handling with Express
Using Fetch to Send Form Data
File Uploads
File Uploads with Fetch
Improving File Upload UI
Conclusion
9
Cookies and Sessions
Externalizing Credentials
Cookies in Express
Examining Cookies
Sessions
Memory Stores
Using Sessions
Using Sessions to Implement Flash Messages
What to Use Sessions For
Conclusion
10
Middleware
Middleware Principles
Middleware Examples
Common Middleware
Third-Party Middleware
Conclusion
11
Sending Email
SMTP, MSAs, and MTAs
Receiving Email
Email Headers
Email Formats
HTML Email
Nodemailer
Sending Mail
Sending Mail to Multiple Recipients
Better Options for Bulk Email
Sending HTML Email
Images in HTML Email
Using Views to Send HTML Email
Encapsulating Email Functionality
Conclusion
12
Production Concerns
Execution Environments
Environment-Specific Configuration
Running Your Node Process
Scaling Your Website
Scaling Out with App Clusters
Handling Uncaught Exceptions
Scaling Out with Multiple Servers
Monitoring Your Website
Third-Party Uptime Monitors
Stress Testing
Conclusion
13
Persistence
Filesystem Persistence
Cloud Persistence
Database Persistence
A Note on Performance
Abstracting the Database Layer
Setting Up MongoDBThe difficulty involved
Mongoose
Database Connections with Mongoose
Creating Schemas and Models
Seeding Initial Data
Retrieving Data
Adding Data
PostgreSQL
Adding Data
Using a Database for Session Storage
Conclusion
14
Routing
Routes and SEO
Subdomains
Route Handlers Are Middleware
Route Paths and Regular Expressions
Route Parameters
Organizing Routes
Declaring Routes in a Module
Grouping Handlers Logically
Automatically Rendering Views
Conclusion
15
REST APIs and JSON
JSON and XML
Our API
API Error Reporting
Cross-Origin Resource Sharing
Our Tests
Using Express to Provide an API
Conclusion
16
Single-Page Applications
A Short History of Web Application Development
SPA Technologies
Creating a React App
React Basics
The Home Page
Routing
Vacations Page—Visual Design
Sending Information to the Server
State Management
Deployment Options
Conclusion
17
Static Content
Performance Considerations
Content Delivery Networks
Designing for CDNs
Server-Rendered Website
Single-Page Applications
Caching Static Assets
Changing Your Static Content
Conclusion
18
Security
HTTPS
Generating Your Own Certificate
Using a Free Certificate Authority
Purchasing a Certificate
Enabling HTTPS for Your Express App
A Note on Ports
HTTPS and Proxies
Cross-Site Request Forgery
Authentication
Authentication Versus Authorization
The Problem with Passwords
Third-Party Authentication
Storing Users in Your Database
Authentication Versus Registration and the User Experience
Passport
Role-Based Authorization
Adding Authentication Providers
Conclusion
19
Integrating with Third-Party APIs
Social Media
Social Media Plugins and Site Performance
Searching for Tweets
Rendering Tweets
Geocoding
Geocoding with Google
Geocoding Your Data
Displaying a Map
Weather Data
Conclusion
20
Debugging
The First Principle of Debugging
Take Advantage of REPL and the Console
Using Node’s Built-in Debugger
Node Inspector Clients
Debugging Asynchronous Functions
Debugging Express
Conclusion
21
Going Live
Domain Registration and Hosting
Domain Name System
Security
Top-Level Domains
Subdomains
Nameservers
Hosting
Deployment
Conclusion
22
Maintenance
The Principles of Maintenance
Have a Longevity Plan
Use Source Control
Use an Issue Tracker
Exercise Good Hygiene
Don’t Procrastinate
Do Routine QA Checks
Monitor Analytics
Optimize Performance
Prioritize Lead Tracking
Prevent “Invisible” Failures
Code Reuse and Refactoring
Private npm Registry
Middleware
Conclusion
23
Additional Resources
Online Documentation
Periodicals
Stack Overflow
Contributing to Express
Conclusion
Index