路由
路由是指如何定義應用的端點(URIs)以及如何響應客戶端的請求。
路由是由一個 URI、HTTP 請求(GET、POST等)和若干個句柄組成,它的結構如下:
app.METHOD(path, [callback...], callback),app是express對象的一個實例,METHOD是一個[HTTP 請求方法(http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol),path是服務器上的路徑,callback是當路由匹配時要執(zhí)行的函數(shù)。
下面是一個基本的路由示例:
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');
});
路由方法
其中最常用的就是get 和 post兩個請求示例如下:
// 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');
});
有些路由方法名不是合規(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(); // pass control to the next handler
});
路由路徑
路由路徑和請求方法一起定義了請求的端點,它可以是字符串、字符串模式或者正則表達式。
前面我們寫的都是字符串類型的,下面我們分別看一下字符串模式、正則表達式的方式。
使用字符串模式的路由路徑示例:
// 匹配 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$/');
});
路由句柄
可以為請求處理提供多個回調函數(shù),其行為類似中間件。唯一的區(qū)別是這些回調函數(shù)有可能調用next('route')方法而略過其他路由回調函數(shù)??梢岳迷摍C制為路由定義前提條件,如果在現(xiàn)有路徑上繼續(xù)執(zhí)行沒有意義,則可將控制權交給剩下的路徑。
路由句柄有多種形式,可以是一個函數(shù)、一個函數(shù)數(shù)組,或者是兩者混合,如下所示.
使用多個回調函數(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!');
});
使用回調函數(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!');
});
app.route()
可使用app.route() 創(chuàng)建路由路徑的鏈式路由句柄。由于路徑在一個地方指定,這樣做有助于創(chuàng)建模塊化的路由,而且減少了代碼冗余和拼寫錯誤。請參考Router() 文檔 了解更多有關路由的信息。
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
可使用 express.Router 類創(chuàng)建模塊化、可掛載的路由句柄。Router 實例是一個完整的中間件和路由系統(tǒng),因此常稱其為一個 “mini-app”。
下面的實例程序創(chuàng)建了一個路由模塊,并加載了一個中間件,定義了一些路由,并且將它們掛載至應用的路徑上。
在 app 目錄下創(chuàng)建名為 birds.js 的文件,內容如下:
var express = require('express');
var router = express.Router();
// 該路由使用的中間件
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now());
next();
});
// 定義網(wǎng)站主頁的路由
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 的請求,并且調用為該路由指定的 timeLog 中間件。