Express.js 4 Cheatsheet
Installation
$ sudo npm install express
: install the latest Express.js locally$ sudo npm install express@4.2.0 --save
: install Express.js v4.2.0 locally and save topackage.json
$ sudo npm install -g express-generator@4.0.0
: install Express.js command-line generator v4.0.0
Generator
Usage: $ express [options] [dir]
Options:
-h
: print the usage information-V
: print the express-generator version number-e
: add ejs engine support, defaults to jade if omitted-H
: add hogan.js engine support-c <library>
add CSS support for (less|stylus|compass), defaults to plain CSS if omitted-f
: generate into a non-empty directory
Basics
var express = require('express')
: include the modulevar app = express()
: create an instanceapp.listen(portNumber, callback)
: start the Express.js serverhttp.createServer(app).listen(portNumber, callback)
: start the Express.js serverapp.set(key, value)
: set a property value by the keyapp.get(key)
: get a property value by the key
HTTP Verbs and Routes
app.get(urlPattern, requestHandler[, requestHandler2, ...])
app.post(urlPattern, requestHandler[, requestHandler2, ...])
app.put(urlPattern, requestHandler[, requestHandler2, ...])
app.delete(urlPattern, requestHandler[, requestHandler2, ...])
app.all(urlPattern, requestHandler[, requestHandler2, ...])
app.param([name,] callback)
:app.use([urlPattern,] requestHandler[, requestHandler2, ...])
Request
request.params
: parameters middlwarerequest.param
: extract one parameterrequest.query
: extract query string parameterrequest.route
: return route stringrequest.cookies
: cookies, requirescookie-parser
request.signedCookies
: signed cookies, requirescookie-parser
request.body
: payload, requiresbody-parser
Request Header Shortcuts
request.get(headerKey)
: value for the header keyrequest.accepts(type)
: checks if the type is acceptedrequest.acceptsLanguage(language)
: checks languagerequest.acceptsCharset(charset)
: checks charsetrequest.is(type)
: checks the typerequest.ip
: IP addressrequest.ips
: IP addresses (with trust-proxy on)request.path
: URL pathrequest.host
: host without port numberrequest.fresh
: checks freshnessrequest.stale
: checks stalenessrequest.xhr
: true for AJAX-y requestsrequest.protocol
: returns HTTP protocolrequest.secure
: checks if protocol ishttps
request.subdomains
: array of subdomainsrequest.originalUrl
: original URL
Response
response.redirect(status, url)
: redirect requestresponse.send(status, data)
: send responseresponse.json(status, data):
send JSON and force proper headersresponse.sendfile(path, options, callback)
: send a fileresponse.render(templateName, locals, callback)
: render a templateresponse.locals
: pass data to template
Handlers Signatures
function(request, response, next) {}
: request handler signaturefunction(error, request, response, next) {}
: error handler signature
Stylus and Jade
app.use(require('stylus').middleware(path.join(__dirname, 'public')))
Body
Static
app.use(express.static(path.join(__dirname, 'public')))
Connect Middleware
$ sudo npm install <package_name> --save
body-parser request payload
compression gzip
cookie-parser Cookies
cookie-session Session via Cookies store
csurf CSRF
errorhandler error handler
express-session session via in-memory or other store
method-override HTTP method override
morgan server logs
serve-favicon favicon
serve-static static content
Other Popular Middleware
qs: analogous to
query
st, connect-static analogous to
staticCache
express-validator: validation
less: LESS CSS
passport: authentication library
helmet: security headers
cors: CORS
Last updated