Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Preface

Acknowledgments

About this Book

About the Cover Illustration

1. Intro

Chapter 1. What is Express?

1.1. What is this Node.js business?

1.2. What is Express?

1.2.1. The functionality in Node.js

1.2.2. What Express adds to Node.js

1.3. Express’s minimal philosophy

1.4. The core parts of Express

1.4.1. Middleware

1.4.2. Routing

1.4.3. Subapplications

1.4.4. Conveniences

1.5. The ecosystem surrounding Express

1.5.1. Express vs. other web application frameworks

1.5.2. What Express is used for

1.5.3. Third-party modules for Node.js and Express

1.6. The obligatory Hello World

1.7. Summary

Chapter 2. The basics of Node.js

2.1. Installing Node

2.1.1. Running your first Node script

2.2. Using modules

2.2.1. Requiring built-in modules

2.2.2. Requiring third-party modules with package.json and npm

2.2.3. Defining your own modules

2.3. Node: an asynchronous world

2.4. Building a web server with Node: the http module

2.5. Summary

Chapter 3. Foundations of Express

3.1. Middleware

3.1.1. Hello World with Express

3.1.2. How middleware works at a high level

3.1.3. Middleware code that’s passive

3.1.4. Middleware code that changes the request and response

3.1.5. Third-party middleware libraries

3.2. Routing

3.3. Extending request and response

3.4. Views

3.5. Example: putting it all together in a guestbook

3.5.1. Getting set up

3.5.2. The main app code

3.5.3. Creating the views

3.5.4. Start it up

3.6. Summary

2. Core

Chapter 4. Middleware

4.1. Middleware and the middleware stack

4.2. Example app: a static file server

4.2.1. Getting set up

4.2.2. Writing your first middleware function: the logger

4.2.3. The static file server middleware

4.2.4. 404 handler middleware

4.2.5. Switching your logger to an open source one: Morgan

4.2.6. Switching to Express’s built-in static file middleware

4.3. Error-handling middleware

4.4. Other useful middleware

4.5. Summary

Chapter 5. Routing

5.1. What is routing?

5.1.1. A simple routing example

5.2. The features of routing

5.2.1. Grabbing parameters to routes

5.2.2. Using regular expressions to match routes

5.2.3. Grabbing query arguments

5.3. Using routers to split up your app

5.4. Serving static files

5.4.1. Static files with middleware

5.4.2. Routing to static files

5.5. Using Express with HTTPS

5.6. Putting it all together: a simple routing demo

5.6.1. Setting up

5.6.2. The main app code

5.6.3. The two views

5.6.4. The application in action

5.7. Summary

Chapter 6. Building APIs

6.1. A basic JSON API example

6.2. A simple Express-powered JSON API

6.3. Create, read, update, delete APIs

6.3.1. HTTP verbs (also known as HTTP methods)

6.3.2. CRUD applications with HTTP methods

6.4. API versioning

6.5. Setting HTTP status codes

6.5.1. Setting HTTP status codes

6.5.2. The 100 range

6.5.3. The 200 range

6.5.4. The 300 range

6.5.5. The 400 range

6.5.6. The 500 range

6.6. Summary

Chapter 7. Views and templates: Pug and EJS

7.1. Express’s view features

7.1.1. A simple view rendering

7.1.2. A complicated view rendering

7.1.3. Making all view engines compatible with Express: Consolidate.js

7.2. Everything you need to know about EJS

7.2.1. The syntax of EJS

7.3. Everything you need to know about Pug

7.3.1. The syntax of Pug

7.3.2. Layouts in Pug

7.3.3. Mixins in Pug

7.4. Summary

3. Express in Context

Chapter 8. Persisting your data with MongoDB

8.1. Why MongoDB?

8.1.1. How Mongo works

8.1.2. For you SQL users out there

8.1.3. Setting up Mongo

8.2. Talking to Mongo from Node with Mongoose

8.2.1. Setting up your project

8.2.2. Creating a user model

8.2.3. Using your model

8.3. Authenticating users with Passport

8.3.1. Setting up Passport

8.4. Summary

Chapter 9. Testing Express applications

9.1. What is testing and why is it important?

9.1.1. Test-driven development

9.1.2. Cardinal rule: when in doubt, test

9.2. Introducing the Mocha testing framework

9.2.1. How does Node.js testing work?

9.2.2. Setting up Mocha and the Chai assertion library

9.2.3. What happens when you run your tests

9.2.4. Writing your first test with Mocha and Chai

9.2.5. Adding more tests

9.2.6. More features of Mocha and Chai

9.3. Testing Express servers with SuperTest

9.3.1. Testing a simple API

9.3.2. Filling in the code for your first tests

9.3.3. Testing HTML responses

9.4. Summary

Chapter 10. Security

10.1. The security mindset

10.2. Keeping your code as bug-free as possible

10.2.1. Enforcing good JavaScript with JSHint

10.2.2. Halting after errors happen in callbacks

10.2.3. Perilous parsing of query strings

10.3. Protecting your users

10.3.1. Using HTTPS

10.3.2. Preventing cross-site scripting attacks

10.3.3. Cross-site request forgery (CSRF) prevention

10.4. Keeping your dependencies safe

10.4.1. Auditing the code

10.4.2. Keeping your dependencies up to date

10.4.3. Check against the Node Security Project

10.5. Handling server crashes

10.6. Various little tricks

10.6.1. No Express here

10.6.2. Preventing clickjacking

10.6.3. Keeping Adobe products out of your site

10.6.4. Don’t let browsers infer the file type

10.7. Summary

Chapter 11. Deployment: assets and Heroku

11.1. LESS, a more pleasant way to write CSS

11.1.1. Variables

11.1.2. Functions

11.1.3. Mixins

11.1.4. Nesting

11.1.5. Includes

11.1.6. Alternatives to LESS

11.2. Using Browserify to require modules in the browser

11.2.1. A simple Browserify example

11.3. Using Grunt to compile, minify, and more

11.3.1. Installing Grunt

11.3.2. Compiling LESS with Grunt

11.3.3. Using Browserify with Grunt

11.3.4. Minifying the JavaScript with Grunt

11.3.5. Using Grunt watch

11.3.6. Other helpful Grunt tasks

11.4. Using connect-assets to compile LESS and CoffeeScript

11.4.1. Getting everything installed

11.4.2. Setting up the middleware

11.4.3. Linking to assets from views

11.4.4. Concatenating scripts with directives

11.5. Deploying to Heroku

11.5.1. Getting Heroku set up

11.5.2. Making a Heroku-ready app

11.5.3. Deploying your first app

11.5.4. Running Grunt on Heroku

11.5.5. Making your server more crash resistant

11.6. Summary

Chapter 12. Best practices

12.1. Simplicity

12.2. File structure pattern

12.3. Locking down dependency versions

12.3.1. The simple way: eschewing optimistic versioning

12.3.2. The thorough way: npm’s shrinkwrap command

12.3.3. Upgrading and adding dependencies

12.4. Localized dependencies

12.4.1. Invoking commands directly

12.4.2. Executing commands from npm scripts

12.5. Summary

Other helpful modules

Index

List of Figures

List of Listings