国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Express的路由詳解

2019-11-20 11:02:11
字體:
來源:轉載
供稿:網(wǎng)友

路由

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

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

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

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 請求方法,和 express 實例相關聯(lián)。

下面這個例子展示了為應用跟路徑定義的 GET 和 POST 請求:

// 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 請求對應的路由方法: 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(); // 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 和 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');});

字符 ?、+、* 和 () 是正則表達式的子集,- 和 . 在基于字符串的路徑中按照字面值解釋。
使用正則表達式的路由路徑示例:

// 匹配任何路徑中含有 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ù)。可以利用該機制為路由定義前提條件,如果在現(xiàn)有路徑上繼續(xù)執(zhí)行沒有意義,則可將控制權交給剩下的路徑。

路由句柄有多種形式,可以是一個函數(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)的方法向客戶端返回響應,終結請求響應的循環(huán)。如果在路由句柄中一個方法也不調(diào)用,來自客戶端的請求會一直掛起。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阆中市| 靖安县| 黄梅县| 曲靖市| 吉木萨尔县| 晴隆县| 遂川县| 鱼台县| 内江市| 丹凤县| 南川市| 双流县| 温州市| 西贡区| 泰安市| 郧西县| 叶城县| 波密县| 鄂伦春自治旗| 兴业县| 淮南市| 南江县| 府谷县| 航空| 大化| 合肥市| 寿宁县| 高平市| 伊通| 鲁山县| 彩票| 望江县| 大方县| 泗水县| 绵竹市| 班戈县| 旬邑县| 五华县| 通山县| 古交市| 丹凤县|