Skip to content

Instantly share code, notes, and snippets.

@JacobWay
Last active March 12, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobWay/69ec5d24582f550a0aa0b07b9231bfe2 to your computer and use it in GitHub Desktop.
Save JacobWay/69ec5d24582f550a0aa0b07b9231bfe2 to your computer and use it in GitHub Desktop.
node.js express.js notes

Guide

Routing

Routing resfers to the definition of application end points (URIs) and how they response to client requests.

Routing methods

  • express class -> routing methods -> http methods

Routing paths

  • route path based on strings
  • route path based on string patterns
  • route path based on regular expressions

Route parameters

Route handlers

Response methods

  • response.download()
  • response.end()
  • response.json()
  • response.jsonp()
  • response.redirect()
  • response.render()
  • response.send()
  • response.sendFile()
  • response.sendStatus()

app.route()

express.Router

Use the express.Router class to create modular, mountable route handlers.

Writing middleware for use in Express apps

Overview

Middleware -> request, response, next

Middleware function can perform the following tasks:

  • Excute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware in the stack.

Example

Configurable middleware

Refer to cookie-session and compression for examples of configurable middleware.

Using middleware

An Express web application is essentially a series of middleware function calls.

An Express application can use the following types of middleware.

  • Application level middleware
  • Router level middleware
  • Error handling level middleware
  • Building middleware
  • Third party middleware

Application level middleware

  • app.use([mounted path], <middleware function handler>)
  • app.httpMethod([mounted path], <middleware function handler>)

Router level middleware

Router level middleware is bound to an instance of express.Router().

Error handling middleware

Build in middleware

Third party middleware

Using template engines with Express

A template engine enables you to use static template files in your application.

  • app.set("views", "the directory of template files")
  • app.set("view engine", "the name of the view engine")

Error handling

Debugging Express

DEBUG=smaple-app,html,mail,express:* node index.js

Express behind proxies

Database integration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment