?
This document uses PHP Chinese website manual Release
路由是指應(yīng)用程序端點(diǎn)(URI)的定義以及它們?nèi)绾雾憫?yīng)客戶端請求。有關(guān)路由的介紹,請參閱基本路由。
以下代碼是一個(gè)非?;镜穆酚傻睦?。
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')})
路由方法是從其中一個(gè)HTTP方法派生的,并附加到express
該類的一個(gè)實(shí)例。
以下代碼是為GET和POST方法定義的應(yīng)用程序根路徑的示例。
// 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支持對應(yīng)于HTTP方法的下列路由方法:get
,post
,put
,head
,delete
,options
,trace
,copy
,lock
,mkcol
,move
,purge
,unlock
,report
,mkactivity
,checkout
,merge
,m-search
,notify
,subscribe
,unsubscribe
,patch
和search
。
路由轉(zhuǎn)換為無效JavaScript變量名稱的方法,請使用括號表示法。例如,app['m-search']('/', function ...
有一種特殊的路由方法,app.all()
它不是從任何HTTP方法派生的。此方法用于為所有請求方法的路徑提供加載中間件功能。
在以下示例中,無論您是使用GET,POST,PUT,DELETE還是http模塊中支持的任何其他HTTP請求方法,該處理程序都會針對“/ secret”請求執(zhí)行。
app.all('/secret', function (req, res, next) { console.log('Accessing the secret section ...') next() // pass control to the next handler})
路由路徑與請求方法一起定義了可以進(jìn)行請求的端點(diǎn)。路徑路徑可以是字符串,字符串模式或正則表達(dá)式。
字符?
,+
,*
,和()
是他們的正則表達(dá)式的對應(yīng)的子集。字符串路徑會逐字解釋連字符(-
)和點(diǎn)(.
)。
如果您需要在路徑字符串中使用字符($
),請將其包含在([])
之內(nèi)。例如,“ /data/$book
” 處的請求的路徑字符串將是“ /data/([\$])book
”。
Express使用path-to-regexp來匹配路由路徑; 請參閱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
,abRANDOMcd
,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')})
基于正則表達(dá)式的路由路徑示例:
此路由路徑將與路由名稱中的“a”匹配。
app.get(/a/, function (req, res) { res.send('/a/')})
這條路由的路徑將匹配butterfly
和dragonfly
,但不butterflyman
,dragonflyman
等。
app.get(/.*fly$/, function (req, res) { res.send('/.*fly$/')})
路由參數(shù)是指定的URL段,用于捕獲在URL中它們的位置處指定的值。捕獲的值將填充到req.params
對象中,路徑中指定的路由參數(shù)的名稱將作為其各自的鍵。
Route path: /users/:userId/books/:bookId Request URL: http://localhost:3000/users/34/books/8989req.params: { "userId": "34", "bookId": "8989" }
要使用路由參數(shù)定義路由,只需在路由路徑中指定路由參數(shù),如下所示。
app.get('/users/:userId/books/:bookId', function (req, res) { res.send(req.params)})
路線參數(shù)的名稱必須由“單詞字符”(A-Za-z0-9_)組成。
由于連字符(-
)和點(diǎn)(.
)是字面解釋的,因此它們可以與路由參數(shù)一起使用以達(dá)到有用的目的。
Route path: /flights/:from-:to Request URL: http://localhost:3000/flights/LAX-SFO req.params: { "from": "LAX", "to": "SFO" }
Route path: /plantae/:genus.:species Request URL: http://localhost:3000/plantae/Prunus.persica req.params: { "genus": "Prunus", "species": "persica" }
要更好地控制可以通過路由參數(shù)匹配的確切字符串,可以在括號(()
)中追加正則表達(dá)式:
Route path: /user/:userId(\d+)Request URL: http://localhost:3000/user/42req.params: {"userId": "42"}
由于正則表達(dá)式通常是文字字符串的一部分,因此請確保\
使用附加的反斜杠轉(zhuǎn)義任何字符\\d+
。
在Express 4.x中,正則表達(dá)式中的*
字符不以通常的方式解釋。作為解決方法,請使用{0,}
而不是*
。Express 5可能會解決這個(gè)問題。
您可以提供多個(gè)回調(diào)函數(shù),這些函數(shù)的行為與中間件相似,可處理請求。唯一的例外是這些回調(diào)可能會調(diào)用next('route')
繞過剩余的路由回調(diào)。您可以使用此機(jī)制在路線上施加先決條件,然后在沒有理由繼續(xù)當(dāng)前路線的情況下將控制權(quán)交給后續(xù)路線。
路由處理程序可以采用函數(shù),函數(shù)數(shù)組或兩者的組合的形式,如以下示例所示。
一個(gè)回調(diào)函數(shù)可以處理一條路線。例如:
app.get('/example/a', function (req, res) { res.send('Hello from A!')})
多個(gè)回調(diào)函數(shù)可以處理一個(gè)路徑(確保你指定了這個(gè)next
對象)。例如:
app.get('/example/b', function (req, res, next) { console.log('the 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])
獨(dú)立函數(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('the response will be sent by the next function ...') next()}, function (req, res) { res.send('Hello from D!')})
res
下表中響應(yīng)對象()的方法可以向客戶端發(fā)送響應(yīng),并終止請求 - 響應(yīng)循環(huán)。如果這些方法都不是從路由處理程序調(diào)用的,則客戶端請求將被掛起。
方法 | 描述 |
---|---|
res.download() | 提示要下載的文件。 |
res.end() | 結(jié)束響應(yīng)過程。 |
res.json() | 發(fā)送JSON響應(yīng)。 |
res.jsonp() | 用JSONP支持發(fā)送JSON響應(yīng)。 |
res.redirect() | 重定向請求。 |
res.render() | 呈現(xiàn)視圖模板。 |
res.send() | 發(fā)送各種類型的響應(yīng)。 |
res.sendFile() | 以八位字節(jié)流的形式發(fā)送文件。 |
res.sendStatus() | 設(shè)置響應(yīng)狀態(tài)代碼并將其字符串表示形式作為響應(yīng)主體發(fā)送。 |
通過使用可以為路由路徑創(chuàng)建可鏈接的路由處理程序app.route()
。由于路徑是在單個(gè)位置指定的,所以創(chuàng)建模塊化路由很有幫助,因?yàn)檫@可以減少冗余和拼寫錯(cuò)誤。有關(guān)路由的更多信息,請參閱: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)建模塊化,可掛載的路由處理程序。一個(gè)Router
實(shí)例是一個(gè)完整的中間件和路由系統(tǒng); 出于這個(gè)原因,它通常被稱為“迷你應(yīng)用程序”。
以下示例將路由器創(chuàng)建為模塊,在其中加載中間件功能,定義一些路由并將路由器模塊安裝在主應(yīng)用程序的路徑中。
在app目錄中創(chuàng)建一個(gè)birds.js,
在app目錄中命名路由器文件,其中包含以下內(nèi)容:
var express = require('express')var router = express.Router()// middleware that is specific to this routerrouter.use(function timeLog (req, res, next) { console.log('Time: ', Date.now()) next()})// define the home page routerouter.get('/', function (req, res) { res.send('Birds home page')})// define the about routerouter.get('/about', function (req, res) { res.send('About birds')})module.exports = router
然后,在應(yīng)用程序中加載路由器模塊:
var birds = require('./birds')// ...app.use('/birds', birds)
該應(yīng)用程序現(xiàn)在能夠處理對/birds
和/birds/about
的請求,并且可以調(diào)用timeLog
特定于該路線的中間件功能。