Detour is a router for node.js web applications.
Detour is different from sinatra-style routers (like express's router) because you route urls to objects (that have http methods) instead of to http methods.
Rationale: If you have multiple http methods implemented for a given url (like a lot of APIs do), this style of routing will be much more natural and will vastly improve your code organization and re-use. With object routing, it's much simpler to keep the related handlers together, but separated from unrelated handlers (often even in another file/module).
var detour = ;var http = ;var router = ;router;http;
var detour = ;var express = ;var app = ;var router = ;app;router;
A simple example:
In the example above, you can see that the path string sent to the router (
/test/:test_id) uses the same format as express. Regular expressions are also allowed.
You can also see from the example that within a route handler,
req.pathVar will have all the variables collected from your route.
Detour supports connect-style middleware like most node.js frameworks (including express). Example:
var detour = ;var router = ;router;
The middleware stack for detour runs after the route has been
determined, but before any other processing. The entire routed resource
object will be available at
req.detourHandler if you want to do any
special handling in your middleware that are related to its contents.
This makes Detour extremely extensible on a resource-by-resource basis. For example, if you write a resource object like this:
...you can write a middleware that acts according to that data before the handler for GET is called.
In addition to regular object routing, For the sake of convenience, Detour also supports collection-routing where two resource objects can be routed in a collection-member relationship at the same time.
Here's an example:
var router = ;router;
Note: the object being routed has two properties:
member, with each being a resource object on its own. The
resource object will be routed to the given path (
/test/:testid in this
case), and the
collection resource object will be routed to the parent
/test in this case).
This feature can be useful if you have resources that follow this pattern and you want to keep these two similar resources together in the same file.
There is a default OPTIONS response for any route but you can over-ride it like this:
There is a default HEAD response for any route with a GET, but you can over-ride it like this:
The correct http response for a method that an existing url doesn't support is a 405. You can over-ride the default like this:
There is a default 404 response for any route that doesn't match, but you can over-ride it like this:
(NOTE: this only works if the middleware doesn't get a third parameter -- typically called
next() -- passed to it,
next() is used for 404s as you'd expect from a middleware.)