Reading 09 - AvreyBrown-401-advanced-javascript/seattle-javascript-401d32 GitHub Wiki
Routing Routing refers to how an application’s endpoints (URIs) respond to client requests. You define routing using methods of the Express app object that correspond to HTTP methods; for example, app.get() to handle GET requests and app.post to handle POST requests.
Basic Route
var express = require('express') var app = express()
// respond with "hello world" when a GET request is made to the homepage app.get('/', function (req, res) { res.send('hello world') })
Route methods A route method is derived from one of the HTTP methods, and is attached to an instance of the express class.
// GET method route app.get('/', function (req, res) { res.send('GET request to the homepage') })
// POST method route app.post('/', function (req, res) { res.send('POST request to the homepage') })
Route paths Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings, string patterns, or regular expressions.
The characters ?, +, *, and () are subsets of their regular expression counterparts. The hyphen (-) and the dot (.) are interpreted literally by string-based paths.
If you need to use the dollar character ($) in a path string, enclose it escaped within ([ and ]). For example, the path string for requests at “/data/$book”, would be “/data/([$])book”.
Route handlers You can provide multiple callback functions that behave like middleware to handle a request. The only exception is that these callbacks might invoke next('route') to bypass the remaining route callbacks. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current route.