Express路由之一

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

路由是由一個 URI、HTTP 請求(GET、POST等)和若干個句柄組成,它的結構如下: app.METHOD(path, [callback...], callback),app 是 express 對象的一個實例, METHOD 是一個 HTTP 請求方法, path 是服務器上的路徑, callback 是當路由匹配時要執行的函數。

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

var express = require('express');
var app = express();

app.get('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 請求對應的路由方法: 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

有些路由方法名不是合規的 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!');
})


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章