路由
路由是指如何定義應(yīng)用的端點(diǎn)(URIs)以及如何響應(yīng)客戶端的請(qǐng)求。
路由是由一個(gè) URI、HTTP 請(qǐng)求(GET、POST等)和若干個(gè)句柄組成,它的結(jié)構(gòu)如下: app.METHOD(path, [callback...], callback), app 是 express 對(duì)象的一個(gè)實(shí)例, METHOD 是一個(gè) HTTP 請(qǐng)求方法, path 是服務(wù)器上的路徑, callback 是當(dāng)路由匹配時(shí)要執(zhí)行的函數(shù)。
下面是一個(gè)基本的路由示例:
var express = require('express');var app = express();// respond with "hello world" when a GET request is made to the homepageapp.get('/', function(req, res) { res.send('hello world');});
路由方法
路由方法源于 HTTP 請(qǐng)求方法,和 express 實(shí)例相關(guān)聯(lián)。
下面這個(gè)例子展示了為應(yīng)用跟路徑定義的 GET 和 POST 請(qǐng)求:
// GET method routeapp.get('/', function (req, res) { res.send('GET request to the homepage');});// POST method routeapp.post('/', function (req, res) { res.send('POST request to the homepage');});
Express 定義了如下和 HTTP 請(qǐng)求對(duì)應(yīng)的路由方法: get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search, 和 connect。
有些路由方法名不是合規(guī)的 JavaScript 變量名,此時(shí)使用括號(hào)記法,比如: app['m-search']('/', function ...
app.all() 是一個(gè)特殊的路由方法,沒有任何 HTTP 方法與其對(duì)應(yīng),它的作用是對(duì)于一個(gè)路徑上的所有請(qǐng)求加載中間件。
在下面的例子中,來自 “/secret” 的請(qǐng)求,不管使用 GET、POST、PUT、DELETE 或其他任何 http 模塊支持的 HTTP 請(qǐng)求,句柄都會(huì)得到執(zhí)行。
app.all('/secret', function (req, res, next) { console.log('Accessing the secret section ...'); next(); // pass control to the next handler});
路由路徑
路由路徑和請(qǐng)求方法一起定義了請(qǐng)求的端點(diǎn),它可以是字符串、字符串模式或者正則表達(dá)式。
Express 使用 path-to-regexp 匹配路由路徑,請(qǐng)參考文檔查閱所有定義路由路徑的方法。 Express Route Tester 是測(cè)試基本 Express 路徑的好工具,但不支持模式匹配。
查詢字符串不是路由路徑的一部分。
使用字符串的路由路徑示例:
// 匹配根路徑的請(qǐng)求app.get('/', function (req, res) { res.send('root');});// 匹配 /about 路徑的請(qǐng)求app.get('/about', function (req, res) { res.send('about');});// 匹配 /random.text 路徑的請(qǐng)求app.get('/random.text', function (req, res) { res.send('random.text');});使用字符串模式的路由路徑示例:// 匹配 acd 和 abcdapp.get('/ab?cd', function(req, res) { res.send('ab?cd');});// 匹配 abcd、abbcd、abbbcd等app.get('/ab+cd', function(req, res) { res.send('ab+cd');});// 匹配 abcd、abxcd、abRABDOMcd、ab123cd等app.get('/ab*cd', function(req, res) { res.send('ab*cd');});// 匹配 /abe 和 /abcdeapp.get('/ab(cd)?e', function(req, res) { res.send('ab(cd)?e');});
字符 ?、+、* 和 () 是正則表達(dá)式的子集,- 和 . 在基于字符串的路徑中按照字面值解釋。
使用正則表達(dá)式的路由路徑示例:
// 匹配任何路徑中含有 a 的路徑:app.get(/a/, function(req, res) { res.send('/a/');});// 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等app.get(/.*fly$/, function(req, res) { res.send('/.*fly$/');});
路由句柄
可以為請(qǐng)求處理提供多個(gè)回調(diào)函數(shù),其行為類似 中間件。唯一的區(qū)別是這些回調(diào)函數(shù)有可能調(diào)用 next('route') 方法而略過其他路由回調(diào)函數(shù)。可以利用該機(jī)制為路由定義前提條件,如果在現(xiàn)有路徑上繼續(xù)執(zhí)行沒有意義,則可將控制權(quán)交給剩下的路徑。
路由句柄有多種形式,可以是一個(gè)函數(shù)、一個(gè)函數(shù)數(shù)組,或者是兩者混合,如下所示.
使用一個(gè)回調(diào)函數(shù)處理路由:
app.get('/example/a', function (req, res) { res.send('Hello from A!');});
使用多個(gè)回調(diào)函數(shù)處理路由(記得指定 next 對(duì)象):
app.get('/example/b', function (req, res, next) { console.log('response will be sent by the next function ...'); next();}, function (req, res) { res.send('Hello from B!');});
使用回調(diào)函數(shù)數(shù)組處理路由:
var cb0 = function (req, res, next) { console.log('CB0'); next();}var cb1 = function (req, res, next) { console.log('CB1'); next();}var cb2 = function (req, res) { res.send('Hello from C!');}app.get('/example/c', [cb0, cb1, cb2]);
混合使用函數(shù)和函數(shù)數(shù)組處理路由:
var cb0 = function (req, res, next) { console.log('CB0'); next();}var cb1 = function (req, res, next) { console.log('CB1'); next();}app.get('/example/d', [cb0, cb1], function (req, res, next) { console.log('response will be sent by the next function ...'); next();}, function (req, res) { res.send('Hello from D!');});
響應(yīng)方法
下表中響應(yīng)對(duì)象(res)的方法向客戶端返回響應(yīng),終結(jié)請(qǐng)求響應(yīng)的循環(huán)。如果在路由句柄中一個(gè)方法也不調(diào)用,來自客戶端的請(qǐng)求會(huì)一直掛起。