/api/file/123 no match/api/file/123:undelete matched: resource="file", ID="123"/api/file/123:undelete/comments no match
Named with regexp parameters and suffix:
Defined: /api/:resource/:ID(^\d+$)+:cancel
/api/task/123 no match/api/task/123:cancel matched: resource="task", ID="123"/api/task/abc:cancel no match
Named with catch-all parameters match anything until the path end, including the directory index (the '/' before the catch-all). Since they match anything until the end, catch-all parameters must always be the final path element.
Defined: /files/:filepath*
/files no match/files/LICENSE matched: filepath="LICENSE"/files/templates/article.html matched: filepath="templates/article.html"
The value of parameters is saved on the matched.params. Retrieve the value of a parameter by name:
let type = matched.params['type']let id = matched.params['ID']
As mentioned above, you may use regular expressions defining node:
var node = trie.define('/abc/:name([0-9]{2})')assert(trie.match('/abc/47').node === node)
But due to JavaScript String Escape Notation: '\d' === 'd', trie.define('/abc/:name(\d{2})') === trie.define('/abc/:name(d{2})').
trie.define accept a string literal, not a regex literal, the \ maybe be escaped!
var node = trie.define('/abc/:name(\d{2})')trie.match('/abc/47') // nullassert(trie.match('/abc/dd').node === node)
The same for \w, \S, etc.
To use backslash (\) in regular expression you have to escape it manually:
var node = trie.define('/abc/:name(\\w{2})')assert(trie.match('/abc/ab').node === node)
API
const Router = require('toa-router')
new Router(options)
options.root {String}, optional, default to /, define the router's scope.
options.ignoreCase {Boolean}, optional, default to true, ignore case.
options.fixedPathRedirect: {Boolean}, default to true. If enabled, the trie will detect if the current path can't be matched but a handler for the fixed path exists. matched.fpr will returns either a fixed redirect path or an empty string. For example when "/api/foo" defined and matching "/api//foo", The result matched.fpr is "/api/foo".
options.trailingSlashRedirect: {Boolean}, default to true. If enabled, the trie will detect if the current path can't be matched but a handler for the path with (without) the trailing slash exists. matched.tsr will returns either a redirect path or an empty string. For example if /foo/ is requested but a route only exists for /foo, the client is redirected to /foo. For example when "/api/foo" defined and matching "/api/foo/", The result matched.tsr is "/api/foo".
const router = new Router()const APIRouter = new Router({root: '/api'})
Router.prototype.serve(context)
Returns thunk function.
Router.prototype.define(pattern)
Define a route with the url pattern.
router.define('/:type/:id') .get(someHandler) .put(someHandler) .post(someHandler) .del(someHandler)// support all `http.METHODS`: 'get', 'post', 'put', 'head', 'delete', 'options', 'trace', 'copy', 'lock'...
Router.prototype.get(pattern, handler...)
Router.prototype.put(pattern, handler...)
Router.prototype.post(pattern, handler...)
Router.prototype.del(pattern, handler...)
And all http.METHODS
Support generator handler and async/await handler:
router .get('/:type/:id', function * () { // ... }) .put('/:type/:id', async function () { // ... })