Appendix C. Express.js cheatsheet

 

When you develop your own projects, searching on the internet for React documentation and APIs or going back to this book’s chapters to find a single method isn’t efficient. If you’d like to save time and avoid the distractions lurking everywhere on the Net, use this Express cheatsheet as a quick reference.

Print-ready PDF available

In addition to the text version presented here, I’ve created a free beautifully designed, print-ready PDF version of this cheatsheet. You can request this PDF at http://reactquickly.co/resources.

Installing Express.js

  • $ sudo npm install express—Installs the latest Express.js locally
  • $ sudo npm install express@4.2.0 --save—Installs Express.js v4.2.0 locally, and saves it to package.json
  • $ sudo npm install -g express-generator@4.0.0—Installs the Express.js command-line generator v4.0.0

Generator

Usage

$ express [options] [dir]

Options

  • -h—Prints usage information
  • -V—Prints the express-generator version number
  • -e—Adds EJS engine support; defaults to Jade if omitted
  • -H—Adds hogan.js engine support
  • -c <library>—Adds CSS support for <library> (less|stylus|compass); defaults to plain CSS if -c <library> is omitted
  • -f—Generates into a non-empty directory

Basics

HTTP verbs and routes

Requests

Request-header shortcuts

Response

Handler signatures

Stylus and Jade

Body

var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
    extended: true
}))

Static

app.use(express.static(path.join(__dirname, 'public')))

Connect middleware

$ sudo npm install <package_name> --save

Other popular middleware

Resources