Pathways
A simple HTTP RESTful router.
Examples
Using string patterns
Provides a simple string pattern to match. The pattern can specify URL parameters in the form of :parameterName
and wildcards using *
. If the handlerFunction has an argument that matches a URL parameter, the value is passed to the callback. For example, the url /collection/:id
has a url parameter :id
. If the handler function takes id
as a parameter it will automatically be bound i.e. function (id) { }
.
Usage:
pathways;
Example:
var Pathways = ;var http = ; var pathways = ;var server = http; pathways; server;
Using Regular Expressions
This is useful for more complicated matching scenarios. For example, ensuring that a url parameter is only digit characters or a certain length. The result of url.match(RegExp)
is passed to the handler callback.
Usage:
pathways;
Example:
var Pathways = ;var http = ; var pathways = ;var server = http; pathways; server;
Using a function
This is useful if you want to inspect things other than just the URL. You have access to the request and response just like the request handler callback.
Usage:
pathways;
Example:
var Pathways = ; var http = ;var pathways = ;var server = http; var { ;}; var { if thisrequesturl === 0 var id = thisrequesturl; ; else ; }; pathways; server;
Handler callback
The handler callback is invoked for the first registered route that matches via string, function, or regular expression. If no routes match, the server responds with a 404 status code.
pathways;
The handler callback will be invoked with this
set to an object with properties:
request: /* the HTTP request */ response: /* the HTTP response */ router: /* the pathways router */ params: /* the query parameters provided in the HTTP request */
The handler will be provided arguments depending on the filter.
Using strings:
Pathways will try to bind URL parameters to the callback functions with the same name.
Using RegExp:
The callback will have the match collection as its first argument.
Using a function:
The callback will be invoked with every argument provided to the filter callback after the first. For example cb(true, 1, 'a', 'b')
will invoke the route handler with 3 arguments 1, 'a', and 'b'.