路由是指如何定義應用的端點(URIs)以及如何響應客戶端的請求。
路由是由一個 URI、HTTP 請求(GET、POST等)和若干個句柄組成,它的結(jié)構(gòu)如下: app.METHOD(path, [callback...], callback)
, app
是 express
對象的一個實例, METHOD
是一個 HTTP 請求方法, path
是服務器上的路徑, callback
是當路由匹配時要執(zhí)行的函數(shù)。
下面是一個基本的路由示例:
var express = require('express');
var app = express();
// 當主頁收到 GET 請求時,返回 "hello world"。
app.get('/', function(req, res) {
res.send('hello world');
});
路由方法源于 HTTP 請求方法,和 express
實例相關聯(lián)。
下面這個例子展示了為應用跟路徑定義的 GET 和 POST 請求:
// GET 路由
app.get('/', function (req, res) {
res.send('GET request to the homepage');
});
// POST 路由
app.post('/', function (req, res) {
res.send('POST request to the homepage');
});
Express 定義了如下和 HTTP 請求對應的路由方法: 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 變量名,此時使用括號記法,比如 app['m-search']('/', function ...
app.all()
是一個特殊的路由方法,沒有任何 HTTP 方法與其對應,它的作用是對于一個路徑上的所有請求加載中間件。
在下面的例子中,來自 "/secret" 的請求,不管使用 GET、POST、PUT、DELETE 或其他任何 http 模塊支持的 HTTP 請求,句柄都會得到執(zhí)行。
app.all('/secret', function (req, res, next) {
console.log('Accessing the secret section ...');
next(); // 交給下一個句柄處理
});
路由路徑和請求方法一起定義了請求的端點,它可以是字符串、字符串模式或者正則表達式。
Express 使用 path-to-regexp 匹配路由路徑,請參考文檔查閱所有定義路由路徑的方法。 Express Route Tester 是測試基本 Express 路徑的好工具,但不支持模式匹配。
查詢字符串不是路由路徑的一部分。
使用字符串的路由路徑示例:
// 匹配根路徑的請求
app.get('/', function (req, res) {
res.send('root');
});
// 匹配 /about 路徑的請求
app.get('/about', function (req, res) {
res.send('about');
});
// 匹配 /random.text 路徑的請求
app.get('/random.text', function (req, res) {
res.send('random.text');
});
使用字符串模式的路由路徑示例:
// 匹配 acd 和 abcd
app.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 和 /abcde
app.get('/ab(cd)?e', function(req, res) {
res.send('ab(cd)?e');
});
字符 ?、+、* 和 () 是正則表達式的子集,- 和 . 在基于字符串的路徑中按照字面值解釋。
使用正則表達式的路由路徑示例:
// 匹配任何路徑中含有 a 的路徑:
app.get(/a/, function(req, res) {
res.send('/a/');
});
// 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等
app.get(/.*fly$/, function(req, res) {
res.send('/.*fly$/');
});
可以為請求處理提供多個回調(diào)函數(shù),其行為類似 中間件。唯一的區(qū)別是這些回調(diào)函數(shù)有可能調(diào)用 next('route')
方法而略過其他路由回調(diào)函數(shù)??梢岳迷摍C制為路由定義前提條件,如果在現(xiàn)有路徑上繼續(xù)執(zhí)行沒有意義,則可將控制權(quán)交給剩下的路徑。
路由句柄有多種形式,可以是一個函數(shù)、一個函數(shù)數(shù)組,或者是兩者混合,如下所示.
使用一個回調(diào)函數(shù)處理路由:
app.get('/example/a', function (req, res) {
res.send('Hello from A!');
});
使用多個回調(diào)函數(shù)處理路由(記得指定 next
對象):
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!');
});
下表中響應對象(res
)的方法向客戶端返回響應,終結(jié)請求響應的循環(huán)。如果在路由句柄中一個方法也不調(diào)用,來自客戶端的請求會一直掛起。
方法 | 描述 |
---|---|
res.download() | 提示下載文件。 |
res.end() | 終結(jié)響應。 |
res.json() | 發(fā)送一個 JSON 響應。 |
res.jsonp() | 發(fā)送一個支持 JSONP 的 JSON 響應。 |
res.redirect() | 重定向請求。 |
res.render() | 渲染視圖模板。 |
res.send() | 發(fā)送各種類型的響應。 |
res.sendFile | 以八位字節(jié)流的形式發(fā)送文件。 |
res.sendStatus() | 設置響應狀態(tài)代碼,并將其以字符串形式作為響應體的一部分發(fā)送。 |
可使用 app.route()
創(chuàng)建路由路徑的鏈式路由句柄。由于路徑在一個地方指定,這樣做有助于創(chuàng)建模塊化的路由,而且減少了代碼冗余和拼寫錯誤。請參考 Router() 文檔 了解更多有關路由的信息。
下面這個示例程序使用 app.route()
定義了鏈式路由句柄。
app.route('/book')
.get(function(req, res) {
res.send('Get a random book');
})
.post(function(req, res) {
res.send('Add a book');
})
.put(function(req, res) {
res.send('Update the book');
});
可使用 express.Router
類創(chuàng)建模塊化、可掛載的路由句柄。Router
實例是一個完整的中間件和路由系統(tǒng),因此常稱其為一個 "mini-app"。
下面的實例程序創(chuàng)建了一個路由模塊,并加載了一個中間件,定義了一些路由,并且將它們掛載至應用的路徑上。
在 app 目錄下創(chuàng)建名為 birds.js
的文件,內(nèi)容如下:
var express = require('express');
var router = express.Router();
// 該路由使用的中間件
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now());
next();
});
// 定義主頁路由
router.get('/', function(req, res) {
res.send('Birds home page');
});
// 定義 about 路由
router.get('/about', function(req, res) {
res.send('About birds');
});
module.exports = router;
然后在應用中加載路由模塊:
var birds = require('./birds');
...
app.use('/birds', birds);
應用即可處理發(fā)自 /birds
和 /birds/about
的請求,并且調(diào)用為該路由指定的 timeLog
中間件。