new Route()
A route is a holder for a set of criteria which determine whether an HTTP request or failure should be routed
to a handler.
- Source:
Methods
blockingHandler(requestHandler, ordered) → {Route}
Specify a blocking request handler for the route.
This method works just like Route#handler excepted that it will run the blocking handler on a worker thread
so that it won't block the event loop. Note that it's safe to call context.next() from the
blocking handler as it will be executed on the event loop context (and not on the worker thread.
If the blocking handler is ordered it means that any blocking handlers for the same context are never executed
concurrently but always in the order they were called. The default value of ordered is true. If you do not want this
behaviour and don't mind if your blocking handlers are executed in parallel you can set ordered to false.
Parameters:
| Name | Type | Description |
|---|---|---|
requestHandler |
function | the blocking request handler |
ordered |
boolean | if true handlers are executed in sequence, otherwise are run in parallel |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
consumes(contentType) → {Route}
Add a content type consumed by this route. Used for content based routing.
Parameters:
| Name | Type | Description |
|---|---|---|
contentType |
string | the content type |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
disable() → {Route}
Disable this route. While disabled the router will not route any requests or failures to it.
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
enable() → {Route}
Enable this route.
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
failureHandler(failureHandler) → {Route}
Append a failure handler to the route failure handlers list. The router routes failures to failurehandlers depending on whether the various
criteria such as method, path, etc match. When method, path, etc are the same for different routes, You should add multiple
failure handlers to the same route object rather than creating two different routes objects with one failure handler for route
Parameters:
| Name | Type | Description |
|---|---|---|
failureHandler |
function | the request handler |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
getPath() → {string}
- Source:
Returns:
the path prefix (if any) for this route
- Type
- string
handler(requestHandler) → {Route}
Append a request handler to the route handlers list. The router routes requests to handlers depending on whether the various
criteria such as method, path, etc match. When method, path, etc are the same for different routes, You should add multiple
handlers to the same route object rather than creating two different routes objects with one handler for route
Parameters:
| Name | Type | Description |
|---|---|---|
requestHandler |
function | the request handler |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
last() → {Route}
Specify this is the last route for the router.
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
method(method) → {Route}
Add an HTTP method for this route. By default a route will match all HTTP methods. If any are specified then the route
will only match any of the specified methods
Parameters:
| Name | Type | Description |
|---|---|---|
method |
Object | the HTTP method to add |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
methods() → {Array.<Object>}
- Source:
Returns:
the http methods accepted by this route
- Type
- Array.<Object>
order(order) → {Route}
Specify the order for this route. The router tests routes in that order.
Parameters:
| Name | Type | Description |
|---|---|---|
order |
number | the order |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
path(path) → {Route}
Set the path prefix for this route. If set then this route will only match request URI paths which start with this
path prefix. Only a single path or path regex can be set for a route.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | the path prefix |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
pathRegex(path) → {Route}
Set the path prefix as a regular expression. If set then this route will only match request URI paths, the beginning
of which match the regex. Only a single path or path regex can be set for a route.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | the path regex |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
produces(contentType) → {Route}
Add a content type produced by this route. Used for content based routing.
Parameters:
| Name | Type | Description |
|---|---|---|
contentType |
string | the content type |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
remove() → {Route}
Remove this route from the router
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
setRegexGroupsNames(groups) → {Route}
When you add a new route with a regular expression, you can add named capture groups for parameters.
However, if you need more complex parameters names (like "param_name"), you can add parameters names with this function. You have to name capture groups in regex with names: "p0", "p1", "p2", ...
For example: If you declare route with regex \/(?[a-z]*)\/(?[a-z]*) and group names ["param_a", "param-b"]
for uri /hello/world you receive inside pathParams() the parameter param_a = "hello"
However, if you need more complex parameters names (like "param_name"), you can add parameters names with this function. You have to name capture groups in regex with names: "p0", "p1", "p2", ...
For example: If you declare route with regex \/(?
Parameters:
| Name | Type | Description |
|---|---|---|
groups |
Array.<string> | group names |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route
useNormalisedPath(useNormalisedPath) → {Route}
If true then the normalised request path will be used when routing (e.g. removing duplicate /)
Default is true
Parameters:
| Name | Type | Description |
|---|---|---|
useNormalisedPath |
boolean | use normalised path for routing? |
- Source:
Returns:
a reference to this, so the API can be used fluently
- Type
- Route