Chapter 1. What is Express?
Chapter 2. The basics of Node.js
Listing 2.2. Requiring Node’s url module
Listing 2.3. Requiring things into different variable names
Listing 2.4. A simple package.json file
Listing 2.5. An example of the Mustache templating system
Listing 2.6. A simple package.json file
Listing 2.7. Using the Mustache module
Listing 2.8. A function that returns a random integer between 0 and 100
Listing 2.9. random-integer.js
Listing 2.10. Using our module from another file
Listing 2.11. Reading a file from disk
Listing 2.12. Adding a console.log after the asynchronous operations
Listing 2.13. A simple “hello world” web server with Node
Listing 2.14. Parsing the request URL with a request handler function
Chapter 3. Foundations of Express
Listing 3.1. A bare-bones package.json
Listing 3.2. package.json after installing Express with the --save flag
Listing 3.3. Hello, World with Express
Listing 3.4. A Node request handler function
Listing 3.5. Empty middleware that does nothing
Listing 3.6. Logging middleware
Listing 3.7. Adding fake authentication middleware
Listing 3.8. Using Morgan for logging (in app.js)
Listing 3.9. Using express.static (in app.js)
Listing 3.10. Express routing example
Listing 3.11. Grabbing data from routes
Listing 3.13. sendFile example
Listing 3.14. Blacklisting an IP
Listing 3.15. Setting up views with Express
Listing 3.16. A simple EJS file
Listing 3.17. Rendering a view from Express
Listing 3.18. A simple EJS file, rendered
Listing 3.19. package.json for the guestbook
Chapter 4. Middleware
Listing 4.1. The package.json file for your static file application
Listing 4.2. Updated package.json file for your static file application
Listing 4.3. Start app.js for your static file server
Listing 4.4. Fixing your logging middleware
Listing 4.5. Adding static file middleware to the middleware stack
Listing 4.6. Your final middleware: the 404 handler
Listing 4.7. The first version of the static file app (app.js)
Listing 4.8. app.js that now uses Morgan
Listing 4.9. An alternative use of Morgan
Listing 4.10. Replacing your static file middleware with Express’s
Listing 4.11. The next version of your static file app (app.js)
Listing 4.12. A simple app that always sends a file
Listing 4.13. Printing whether a file successfully sent
Listing 4.14. Entering error mode if a file fails to send
Chapter 5. Routing
Listing 5.1. A simple Express app that shows Olivia’s homepage
Listing 5.2. The simplest parameter
Listing 5.3. Using regular expressions for numeric routes
Listing 5.4. Using regular expressions for complex routes
Listing 5.5. UUID-matching routes with a regexp
Listing 5.6. Handling a search query string
Listing 5.7. Routers in action: the main app
Listing 5.8. A sample router definition (at routes/api_router.js)
Listing 5.9. A simple example of express.static
Listing 5.10. Mounting static file middleware
Listing 5.11. Serving static files from multiple directories
Listing 5.12. Serving static files from multiple directories without conflict
Listing 5.13. Sending profile pictures
Listing 5.14. Using HTTPS with an Express app
Listing 5.15. Using HTTP and HTTPS with Express
Listing 5.16. package.json for this application
Listing 5.18. views/header.ejs
Chapter 6. Building APIs
Listing 6.1. package.json for your random number project
Listing 6.2. Your random number app
Listing 6.3. Drilling down into the error handler
Listing 6.4. Handling different HTTP verbs
Listing 6.5. Version 1 of your API, in api1.js
Listing 6.6. The main app code in app.js
Listing 6.7. Version 2 of your API, in api2.js
Listing 6.8. The main app code in app.js
Listing 6.9. Setting the HTTP status code in Express
Listing 6.10. Setting the HTTP status code and sending some JSON
Chapter 7. Views and templates: Pug and EJS
Listing 7.1. Simple view rendering example
Listing 7.2. Complex rendering example
Listing 7.3. Rendering with Walrus
Listing 7.5. A header EJS file
Listing 7.6. A footer EJS file
Listing 7.7. Including a header and footer from EJS
Listing 7.8. A user widget in userwidget.ejs
Listing 7.9. Adding an EJS filter to sum an array
Listing 7.10. A simple Pug example
Listing 7.11. Listing 7.10 rendered as HTML
Listing 7.12. A simple layout file for Pug
Listing 7.13. Using a Pug layout file
Listing 7.14. The output of using a Pug layout
Chapter 8. Persisting your data with MongoDB
Listing 8.1. package.json for LAM
Listing 8.2. Defining the user schema (in models/user.js)
Listing 8.3. Adding a simple method to the user model (in models/user.js)
Listing 8.4. Requiring bcrypt (in models/user.js)
Listing 8.5. Pre-save action to hash the password (in models/user.js)
Listing 8.6. Checking the user’s password (in models/user.js)
Listing 8.7. Creating and exporting the user model (in models/user.js)
Listing 8.8. Finished models/user.js
Listing 8.10. routes.js, to start
Listing 8.11. views/_header.ejs
Listing 8.12. views/_footer.ejs
Listing 8.14. Adding body-parser middleware (to app.js)
Listing 8.15. Adding sign-up routes (in routes.js)
Listing 8.16. views/signup.ejs
Listing 8.17. The profiles route (in routes.js)
Listing 8.18. views/profile.ejs
Listing 8.19. Setting up the middleware for Passport (in app.js)
Listing 8.20. Requiring and using Passport setup (in app.js)
Listing 8.21. Serializing and deserializing users (in setuppassport.js)
Listing 8.22. Requiring the Passport LocalStrategy (in setuppassport.js)
Listing 8.23. Your Passport local strategy (in setuppassport.js)
Listing 8.24. GET /login (in routes.js)
Listing 8.26. Do the login (in routes.js)
Listing 8.27. Logging out (in routes.js)
Listing 8.28. Passing data to views (in routes.js)
Listing 8.29. Middleware for determining if the user is authenticated (in routes.js)
Chapter 9. Testing Express applications
Listing 9.1. A first version of the capitalize function (in capitalize.js)
Listing 9.2. The package.json for the capitalize function
Listing 9.3. Your first test for capitalize (in test/capitalize.js)
Listing 9.4. Another test for capitalize (in test/capitalize.js)
Listing 9.5. Testing capitalization of the empty string (in test/capitalize.js)
Listing 9.6. The new capitalize.js
Listing 9.7. New tests for capitalization (in test/capitalize.js)
Listing 9.8. Testing with the String object
Listing 9.9. Using Mocha’s beforeEach feature
Listing 9.10. Using Chai to test for errors
Listing 9.12. package.json for “What’s My User Agent?”
Listing 9.13. Skeleton of plain-text tests (in test/txt.js)
Listing 9.14. Using SuperTest to check the response (in test/txt.js)
Listing 9.15. Testing that your app returns the right User Agent string (in test/txt.js)
Listing 9.16. Reducing repetition in code with beforeEach (in test/txt.js)
Listing 9.17. Skeleton of app.js
Listing 9.18. First draft of app.js
Listing 9.19. Making app.js return plain text
Listing 9.20. Testing your HTML responses (in test/html.js)
Listing 9.21. Testing for an HTML response (in test/html.js)
Listing 9.22. Getting the HTML response (in test/html.js)
Listing 9.23. What you might be looking for in your HTML responses
Listing 9.24. Parsing HTML with Cheerio (in test/html.js)
Chapter 10. Security
Listing 10.1. A JavaScript file with a bug
Listing 10.2. Grabbing req.query (note: contains bugs!)
Listing 10.3. Don’t assume your queries exist (note: still contains bugs!)
Listing 10.4. Don’t assume your queries aren’t arrays
Listing 10.5. Enforcing HTTPS in Express
Listing 10.6. Using Helmet’s HSTS middleware
Listing 10.7. A first draft of a hacker form
Listing 10.8. Automatically submitting the form
Listing 10.9. Adding CSRF protections
Listing 10.10. Getting the CSRF token
Listing 10.11. Showing the CSRF token in a form
Listing 10.12. Handling CSRF errors
Listing 10.13. A classic npm start script
Listing 10.14. npm start with Forever
Listing 10.15. Keeping your app out of frames
Listing 10.16. The most restrictive crossdomain.xml
Listing 10.17. A malicious script that could be stored as plain text
Chapter 11. Deployment: assets and Heroku
Listing 11.1. Variables in LESS
Listing 11.2. The compiled CSS from listing 11.1
Listing 11.3. Using functions to lighten and darken colors
Listing 11.4. The compiled CSS from listing 11.3
Listing 11.6. The compiled CSS from listing 11.5
Listing 11.7. CSS example with no nesting
Listing 11.8. A simple LESS nesting example
Listing 11.9. Referring to parent selectors in LESS
Listing 11.10. package.json for your simple Browserify example
Listing 11.11. main.js for your simple Browserify example
Listing 11.12. HTML file for your simple Browserify example
Listing 11.13. A script for running the local Grunt
Listing 11.14. A barebones package.json for these examples
Listing 11.15. A skeleton Gruntfile
Listing 11.16. A simple LESS file (in my_css/main.less)
Listing 11.17. Listing 11.16 compiled to CSS
Listing 11.19. A Gruntfile with LESS
Listing 11.20. Static middleware with compiled files
Listing 11.21. A Gruntfile with Browserify
Listing 11.22. A Gruntfile with Browserify, LESS, and Uglify
Listing 11.23. A Gruntfile with watching added
Listing 11.24. Setting up the connect-assets middleware
Listing 11.25. package.json for your Heroku Express app
Listing 11.26. A Hello World Express app (app.js)
Listing 11.27. Running Grunt in a postinstall script
Listing 11.28. Defining a script for running your server in production
Chapter 12. Best practices
Listing 12.1. Example of optimistic versioning in your package.json
Listing 12.2. Example of omitting optimistic versioning in a package.json
Listing 12.3. Express’s (big!) dependency tree
Listing 12.4. Snippet of an example npm-shrinkwrap.json file