1. 程式人生 > >Express應用程式之路由詳解

Express應用程式之路由詳解

路由

路由是指如何定義應用的端點(URIs)以及如何響應客戶端的請求。

路由是由一個 URI、HTTP 請求(GET、POST等)和若干個控制代碼組成,它的結構如下: app.METHOD(path, [callback...], callback), app 是 express物件的一個例項, METHOD 是一個 HTTP 請求方法, path 是伺服器上的路徑, callback 是當路由匹配時要執行的函式。

下面是一個基本的路由示例:

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'); });

路由方法

路由方法源於 HTTP 請求方法,和 express 例項相關聯。

下面這個例子展示了為應用跟路徑定義的 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'
); });

Express 定義瞭如下和 HTTP 請求對應的路由方法: getpostputheaddeleteoptionstracecopylockmkcolmovepurgepropfindproppatchunlockreportmkactivitycheckoutmergem-searchnotifysubscribeunsubscribepatchsearch, 和 connect

有些路由方法名不是合規的 JavaScript 變數名,此時使用括號記法,比如: app['m-search']('/', function ...

app.all() 是一個特殊的路由方法,沒有任何 HTTP 方法與其對應,它的作用是對於一個路徑上的所有請求載入中介軟體。

在下面的例子中,來自 “/secret” 的請求,不管使用 GET、POST、PUT、DELETE 或其他任何 http 模組支援的 HTTP 請求,控制代碼都會得到執行。

app.all('/secret', function (req, res, next) {
  console.log('Accessing the secret section ...');
  next(); // pass control to the next handler
});

路由路徑

路由路徑和請求方法一起定義了請求的端點,它可以是字串、字串模式或者正則表示式。

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$/');
});

路由控制代碼

可以為請求處理提供多個回撥函式,其行為類似 中介軟體。唯一的區別是這些回撥函式有可能呼叫 next('route') 方法而略過其他路由回撥函式。可以利用該機制為路由定義前提條件,如果在現有路徑上繼續執行沒有意義,則可將控制權交給剩下的路徑。

路由控制代碼有多種形式,可以是一個函式、一個函式陣列,或者是兩者混合,如下所示.

使用一個回撥函式處理路由:

app.get('/example/a', function (req, res) {
  res.send('Hello from A!');
});

使用多個回撥函式處理路由(記得指定 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!');
});

使用回撥函式陣列處理路由:

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]);

混合使用函式和函式陣列處理路由:

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)的方法向客戶端返回響應,終結請求響應的迴圈。如果在路由控制代碼中一個方法也不呼叫,來自客戶端的請求會一直掛起。

app.route()

可使用 app.route() 建立路由路徑的鏈式路由控制代碼。由於路徑在一個地方指定,這樣做有助於建立模組化的路由,而且減少了程式碼冗餘和拼寫錯誤。請參考 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

可使用 express.Router 類建立模組化、可掛載的路由控制代碼。Router 例項是一個完整的中介軟體和路由系統,因此常稱其為一個 “mini-app”。

下面的例項程式建立了一個路由模組,並載入了一箇中間件,定義了一些路由,並且將它們掛載至應用的路徑上。

在 app 目錄下建立名為 birds.js 的檔案,內容如下:

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);

應用即可處理髮自 /birds 和 /birds/about 的請求,並且呼叫為該路由指定的 timeLog 中介軟體。