$ npm i express
: install the latest Express.js locally$ npm i express@4.2.0 --save
: install Express.js v4.2.0 locally and save topackage.json
const express = require('express')
: include the moduleconst 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
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.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.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.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
function(request, response, next) {}
: request handler signaturefunction(error, request, response, next) {}
: error handler signature
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
app.use(require('stylus').middleware(path.join(__dirname, 'public')))
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended : true}))
app.use(express.static(path.join(__dirname, 'public')))
$ npm i -S <package_name>
- body-parser request payload
- compression gzip
- connect-timeout
- 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
- response-time
- serve-favicon favicon
- serve-index
- serve-static static content
- vhost
- cookies and keygrip: analogous to
cookie-parser
- raw-body
- connect-multiparty, connect-busboy
- qs: analogous to
query
- st, connect-static analogous to
staticCache
- express-validator: validation
- less: LESS CSS
- passport: authentication library
- helmet: security headers
- connect-cors: CORS
- connect-redis