Express in Action
Writing, building, and testing Node.js applications
Evan M. Hahn
  • April 2016
  • ISBN 9781617292422
  • 256 pages
  • printed in black & white

Chock-full of helpful examples for both the novice and advanced user.

Jeff Smith, Single Source Systems

Express in Action is a carefully designed tutorial that teaches you how to build web applications using Node and Express.

Table of Contents detailed table of contents

Part 1 Introduction

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

1.3. Express’s minimal philosophy

1.4. The core parts of Express

1.4.1. Middleware

1.4.2. Routing

1.4.3. Sub-applications

1.4.4. Conveniences

1.5. The ecosystem surrounding Express

1.5.1. Express versus other web application frameworks

1.5.2. What Express is used for

1.5.3. Third-party modules for Node and Express

1.6. The obligatory hello world

1.7. Summary

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

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

Part 2 Core Express

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 our first middleware function: the logger

4.2.3. The static file server middleware

4.2.4. 404 handler middleware

4.2.5. Switching our 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

5. Routing

5.1. What is routing?

5.1.1. A simple 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. Getting set up

5.6.2. The main app code

5.6.3. The two views

5.6.4. The application in action

5.7. Summary

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

Part 3 Express in Context

7. Views & Templates: Jade & 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 Jade

7.3.1. The syntax of Jade

7.3.2. Layouts in Jade

7.3.3. Mixins in Jade

7.4. Summary

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 MongoDB from Node with Mongoose

8.2.1. Setting up your project

8.2.2. Creating a user model

8.2.3. Using our model

8.3. Authenticating users with Passport

8.3.1. Setting up Passport

8.4. Summary

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. Introduction to 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 we run our 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 our first tests

9.3.3. Testing HTML responses

9.4. Summary

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 (XSS)

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 filetype

10.7. Summary

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, just like in Node

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. "grunt watch"

11.3.6. Other helpful Grunt tasks

11.4. Using connect-assets to compile LESS and CoffeeScript and more

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 our first app

11.5.4. Running Grunt on Heroku

11.5.5. Making your server more crash-resistant

11.6. Summary

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

Appendixes

Appendix A: Other Helpful Modules

About the Technology

Express.js is a web application framework for Node.js. Express organizes your server-side JavaScript into testable, maintainable modules. It provides a powerful set of features to efficiently manage routes, requests, and views along with beautiful boilerplate for your web applications. Express helps you concentrate on what your application does instead of managing time-consuming technical details.

About the book

Express in Action teaches you how to build web applications using Node and Express. It starts by introducing Node’s powerful traits and shows you how they map to the features of Express. You'll explore key development techniques, meet the rich ecosystem of companion tools and libraries, and get a glimpse into its inner workings. By the end of the book, you'll be able to use Express to build a Node app and know how to test it, hook it up to a database, and automate the dev process.

What's inside

  • Simplify Node app setup with Express
  • Testing Express applications
  • Use Express for easy access to Node features
  • Data storage with MongoDB
  • Covers Express 4 and Express 5 alpha

About the reader

To get the most out of this book, you’ll need to know the basics of web application design and be proficient with JavaScript.

About the author

Evan Hahn is an active member of the Node and Express community and contributes to many open source JavaScript projects.


Buy
  • combo $39.99 pBook + eBook
  • eBook $31.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

Everything you need to know to develop robust applications

Paul Shipley, DTS Food Laboratories

Take the pain out of Node.js with Express in Action!

William E. Wheeler, author of Spring in Practice

The fast track to developing versatile HTTP applications.

Ruben Verborgh, Ghent University – iMinds