Table of Contents
Foreword xiii
Preface xv
1 Introducing Express 1
The JavaScript Revolution 1
Introducing Express 2
A Brief History of Express 4
Upgrading to Express 4.0 4
Node: A New Kind of Web Server 5
The Node Ecosystem 6
Licensing 7
2 Getting Started with Node 9
Getting Mode 9
Using the Terminal 10
Editors 11
npm 12
A Simple Web Server with Node 15
Hello World 14
Event-Driven Programming 14
Routing 15
Serving Static Resources 15
Onward to Express 17
3 Saving Time with Express 19
Scaffolding 19
The Meadowlark Travel Website 20
Initial Steps 20
Views and Layouts 24
Static Files and Views 26
Dynamic Content in Views 27
Conclusion 28
4 Tidying Up 29
Best Practices 29
Version Control 30
How to Use Git with This Book 31
If You're Following Along by Doing It Yourself 31
If You're Following Along by Using the Official Repository 32
npm Packages 33
Project Metadata 34
Node Modules 34
5 Quality Assurance 37
QA: Is ft Worth It? 38
Logic Versus Presentation 39
The Types of Tests 39
Overview of QA Techniques 40
Running Your Server 40
Page Testing 41
Cross-Page Testing 44
Logic Testing 47
Linking 48
Link Checking 49
Automating with Grunt 49
Continuous Integration (CI) 52
6 The Request and Response Objects 53
The Parts of a URL 53
HTTP Request Methods 54
Request Headers 55
Response Headers 55
Internet Media Types 56
Request Body 56
Parameters 57
The Request Object 57
The Response Object 59
Getting More Information 60
Roiling It Down 61
Rendering Content 61
Processing Forms 63
Providing an API 64
7 Templating with Handlebars 67
There Are No Absolute Rules Except This One 68
Choosing a Template Engine 69
Jade: A Different Approach 69
Handlebars Basics 7
Comments 72
Blocks 72
Server-Side Templates 74
Views and Layouts 74
Using Layouts (or Not) in Express 76
Partials
Sections 79
Perfecting Your Templates 80
Client-Side Handlebars 31
Conclusion 83
8 Form Handling 85
Sending Client Data to the Server 85
HTML Forms 85
Encoding 86
Different Approaches to Form Handling 87
Form Handling with Express 89
Handling AJAX Forms 90
File Uploads 93
jQuery File Upload 95
9 Cookies and Sessions 99
Externalizing Credentials 100
Cookies in Express 101
Examining Cookies 103
Sessions 103
Memory Stores 103
Using Sessions 104
Using Sessions to Implement Flash Messages 105
What to Use Sessions For 106
10 Middleware 109
Common Middleware 114
Third Party Middleware 116
11 Sending Email 117
SMTP, MSAs, and MTAs 117
Receiving Email 118
Email Headers 118
Email Formats 119
HTML Email 119
Nodemailer 120
Sending Mail 121
Sending Mail to Multiple Recipients 121
Better Options for Bulk Email 122
Sending HTML Email 122
Images in HTML Email 123
Using Views to Send HTML Email 124
Encapsulating Email Functionality 126
Email as a Site Monitoring Tool 127
12 Production Concerns 129
Execution Environments 129
Environment-Specific Configuration 130
Scaling Your Website 131
Scaling Out with App Clusters 132
Handling Uncaught Exceptions 135
Scaling Out with Multiple Servers 138
Monitoring Your Website 139
Third-Party Uptime Monitors 139
Application Failures 140
Stress Testing 140
13 Persistence 143
Filesystem Persistence 143
Cloud Persistence 145
Database Persistence 146
A Note on Performance 146
Setting Up MongoDB 147
Mongoose 147
Database Connections with Mongoose 148
Creating Schemas and Models 149
Seeding Initial Data 150
Retrieving Data 151
Adding Data 152
Using MongoDB for Session Storage 154
14 Routing 157
Routes and SEO 159
Subdomains 159
Route Handlers Are Middleware 160
Route Paths and Regular Expressions 162
Route Parameters 162
Organizing Routes 163
Declaring Routes in a Module 164
Grouping Handlers Logically 165
Automatically Rendering Views 166
Other Approaches to Route Organization 167
15 Rest APIs and Json 169
JSON and XML 170
Our API 170
API Error Reporting 171
Cross-Origin Resource Sharing (CORS) 172
Our Data Store 173
Our Tests 173
Using Express to Provide an API 175
Using a REST Plugin 176
Using a Subdomain 178
16 Static Content 181
Performance Considerations 182
Future-Proofing Your Website 182
Static Mapping 183
Static Resources in Views 185
Static Resources in CSS 185
Static Resources in Server-Side JavaScript 187
Static Resources in Client-Side JavaScript 187
Serving Static Resources 189
Changing Your Static Content 190
Bundling and Minification 190
Skipping Bundling and Minification in Development Mode 193
A Note on Third-Party Libraries 195
QA 195
Summary 197
17 Implementing MVC in Express 199
Models 200
View Models 201
Controllers 203
Conclusion 205
18 Security 207
HTTPS 207
Generating Your Own Certificate 208
Using a Free Certificate Authority 209
Purchasing a Certificate 210
Enabling HTTPS for Your Express App 212
A Note on Ports 213
HTTPS and Proxies 214
Cross-Site Request Forgery 215
Authentication 216
Authentication Versus Authorization 216
The Problem with Passwords 217
Third-Party Authentication 217
Storing Users in Your Database 218
Authentication Versus Registration and the User Experience 219
Passport 220
Role-Based Authorization 229
Adding Additional Authentication Providers 231
Conclusion 232
19 Integrating with Third-Party APIs 233
Social Media 233
Social Media Plugins and Site Performance 233
Searching for Tweets 234
Rendering Tweets 237
Geocoding 241
Geocoding with Google 241
Geocoding Your Data 242
Displaying a Map 245
Improving Client-Side Performance 247
Weather Data 248
Conclusion 250
20 Debugging 251
The First Principle of Debugging 251
Take Advantage of REPL and the Console 252
Using Node's Built-in Debugger 253
Node Inspector 253
Debugging Asynchronous Functions 257
Debugging Express 257
21 Going Live 261
Domain Registration and Hosting 261
Domain Name System 262
Security 262
Top-Level Domains 263
Subdomains 264
Nameservers 265
Hosting 266
Deployment 269
Conclusion 272
22 Maintenance 273
The Principles of Maintenance 273
Have a Longevity Plan 273
Use Source Control 275
Use an Issue Tracker 275
Exercise Good Hygiene 275
Don't Procrastinate 276
Do Routine QA Checks 276
Monitor Analytics 277
Optimize Performance 277
Prioritize Lead Tracking 277
Prevent "Invisible" Failures 279
Code Reuse and Refactoring 279
Private npm Registry 280
Middleware 281
Conclusion 283
23 Additional Resources 285
Online Documentation 285
Periodicals 286
Stack Overflow 286
Contributing to Express 288
Conclusion 290
Index 291