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 [email protected] --save—Installs Express.js v4.2.0 locally, and saves it to package.json
- $ sudo npm install -g [email protected]—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
- var express = require('express')—Includes a module
- var app = express()—Creates an instance
- app.listen(portNumber, callback)—Starts the Express.js server
- http.createServer(app).listen(portNumber, callback)—Starts the Express.js server
- app.set(key, value)—Sets a property value by key
- app.get(key)—Gets a property value by key
HTTP verbs and routes
- app.get(urlPattern, requestHandler[, requestHandler2, ...])—Handles GET method requests
- app.post(urlPattern, requestHandler[, requestHandler2, ...])—Handles POST method requests
- app.put(urlPattern, requestHandler[, requestHandler2, ...])—Handles PUT method requests
- app.delete(urlPattern, requestHandler[, requestHandler2, ...])—Handles DELETE method requests
- app.all(urlPattern, requestHandler[, requestHandler2, ...])—Handles all method requests
- app.param([name,] callback)—Processes URL parameters
- app.use([urlPattern,] requestHandler[, requestHandler2, ...])—Applies middleware
Requests
- request.params—Parameter middleware
- request.param—Extracts one parameter
- request.query—Extracts a query string parameter
- request.route—Returns a route string
- request.cookies—Accesses cookies; requires cookie-parser
- request.signedCookies—Accesses signed cookies; requires cookie-parser
- request.body—Reads a payload; requires body-parser
Request-header shortcuts
- request.get(headerKey)—Reads Value for the header key
- request.accepts(type)—Checks whether the type is accepted
- request.acceptsLanguage(language)—Checks the language
- request.acceptsCharset(charset)—Checks the character set
- request.is(type)—Checks the type
- request.ip—Reads an IP address
- request.ips—Reads IP addresses (with trust-proxy on)
- request.path—Reads a URL path
- request.host—Accesses a host without a port number
- request.fresh—Checks freshness
- request.stale—Checks staleness
- request.xhr—Checks for XHR/AJAX-y requests
- request.protocol—Returns an HTTP protocol
- request.secure—Checks whether protocol is https
- request.subdomains—Reads an array of subdomains
- request.originalUrl—Reads the original URL
Response
- response.redirect(status, url)—Redirects a request
- response.send(status, data)—Sends a response
- response.json(status, data)—Sends JSON, and forces proper headers
- response.sendfile(path, options, callback)—Sends a file
- response.render(templateName, locals, callback)—Renders a template
- response.locals—Passes data to the template
Handler signatures
- function(request, response, next) {}—Request-handler signature
- function(error, request, response, next) {}—Error-handler signature
Stylus and Jade
Install Jade and Stylus:
$ npm i -SE stylus jade
Apply the Jade template engine:
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
Apply the Stylus CSS processor:
app.use(require('stylus').middleware(path.join(__dirname, 'public')))
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