Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Latest commit

 

History

History
126 lines (97 loc) · 4.94 KB

express4.md

File metadata and controls

126 lines (97 loc) · 4.94 KB

Express 4

Installation

  • $ 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 to package.json

Basics

  • const express = require('express'): include the module
  • const app = express(): create an instance
  • app.listen(portNumber, callback): start the Express.js server
  • http.createServer(app).listen(portNumber, callback): start the Express.js server
  • app.set(key, value): set a property value by the key
  • app.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 middlware
  • request.param: extract one parameter
  • request.query: extract query string parameter
  • request.route: return route string
  • request.cookies: cookies, requires cookie-parser
  • request.signedCookies: signed cookies, requires cookie-parser
  • request.body: payload, requires body-parser

Request Header Shortcuts

  • request.get(headerKey): value for the header key
  • request.accepts(type): checks if the type is accepted
  • request.acceptsLanguage(language): checks language
  • request.acceptsCharset(charset): checks charset
  • request.is(type): checks the type
  • request.ip: IP address
  • request.ips: IP addresses (with trust-proxy on)
  • request.path: URL path
  • request.host: host without port number
  • request.fresh: checks freshness
  • request.stale: checks staleness
  • request.xhr: true for AJAX-y requests
  • request.protocol: returns HTTP protocol
  • request.secure: checks if protocol is https
  • request.subdomains: array of subdomains
  • request.originalUrl: original URL

Response

  • response.redirect(status, url): redirect request
  • response.send(status, data): send response
  • response.json(status, data): send JSON and force proper headers
  • response.sendfile(path, options, callback): send a file
  • response.render(templateName, locals, callback): render a template
  • response.locals: pass data to template

Handlers Signatures

  • function(request, response, next) {}: request handler signature
  • function(error, request, response, next) {}: error handler signature

Stylus and Jade

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')

app.use(require('stylus').middleware(path.join(__dirname, 'public')))

Body

const 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

$ npm i -S <package_name>

Other Popular Middleware