These people are the Sparta’s walls.
Beautifully-designed print-ready PDF
$ 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
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
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
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')))
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(express.static(path.join(__dirname, 'public')))
$ sudo npm install <package_name> --save
- 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
-
qs: analogous to
query
-
st, connect-static analogous to
staticCache
-
express-validator: validation
-
less: LESS CSS
-
passport: authentication library
-
helmet: security headers
-
cors: CORS