Constructor
new Router(handleropt)
- Description:
Create a new router with default fallback handler
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
handler |
function |
<optional> |
Methods
add(method, path, handler)
- Description:
Inserts a path into the Trie.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
method |
string | to handle, optional sorting ID can be set as |
path |
string | to handle |
handler |
any | associated data |
Example
router.add("GET", "/api/info", middleware1)
router.add("*", "/api/", middleware2)
all(path)
- Description:
Add a route to all methods
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | |
...handlers |
function() |
Example
router.all("/api/info", middleware1)
del(method, path, handleropt) → {Array.<object>}
- Description:
Delete a handler from the Trie, must match explicitly by method, path with optional handler
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
method |
string | to handle |
|
path |
string | to handle |
|
handler |
any |
<optional> |
associated data, if missing all handlers will be deleted |
Returns:
| Type | Description |
|---|---|
| Array.<object> |
|
Example
router.del("GET", "/api/info", middleware1)
router.del("*", "/api/")
delete(path)
- Description:
Add DELETE route
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | |
...handlers |
function() |
find(path) → {Array.<object>}
- Description:
Retrieves all nodes that match a path.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | to check. |
Returns:
| Type | Description |
|---|---|
| Array.<object> |
matched routes { route, params }, ... |
get(path, handler)
- Description:
Add GET route
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | |
handler |
function() |
Example
router.get("/api/get", middleware1)
handle(context, next)
- Description:
Handle a request
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
context |
RequestContext | |
next |
function() | (context, err) called after all routes processed but none returned response or error, can be chained to next middleware |
onFinish()
- Description:
Default handler to call if no routes matched or error detected
- Source:
patch(path, handler)
- Description:
Add PATCH route
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | |
handler |
function() |
post(path, handler)
- Description:
Add POST route
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | |
handler |
function() |
Example
router.post("/api/update", middleware1)
put(path)
- Description:
Add PUT route
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | |
...handlers |
function() |
reset()
- Description:
Clear and reset
- Source:
split()
- Description:
Split the path by separator, if empty return separator for explicit root match
- Source:
use(methodopt, pathopt)
- Description:
Add a route or midleware handlers
- Source:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
method |
string |
<optional> |
''
|
empty for all, or a valid HTTP method. Optional sorting ID can be appended as |
path |
string |
<optional> |
*
|
|
..handlers |
function() | object | middleware handlers, if an object it must have method |
Example
router.use(middleware1, middleware2)
router.use("GET", "/api", midleware1)
router.use("/api", midleware1)
// This will be the first route even if added last
router.use("GET#0", /api", midleware5)
walk(callback)
- Description:
Walk the trie non-recursively, call the callback for each leaf node
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
function() |