Resource-oriented routing: Route to objects instead of functions for better code-reuse and organization.
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 = require'detour';var http = require'http';var router = detour;routerroute'/'resend"GET works!";resend"POST works too!";;httpcreateServerroutermiddlewarereq res;listen9999;
var detour = require'detour';var express = require'express';var app = express;var router = detour;appuseroutermiddleware;routerroute'/'resend"GET works!";resend"POST works too!";;
A simple example:
routerroute'/test/:test_id'resend"GET works: " + reqpathVartest_id;// a request to /test/1234 will have output like this:// GET works: 1234// req.pathVar is a hash of all name value pairs pull from the// request url;
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 = require'detour';var router = detour;routerusesomeMiddleware;
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:
routerroute'/test/:test_id'mustBeAuthenticated : true // <-- NB: non-standard propertyresend"GET works: " + reqpathVartest_id;;
...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 = ;routercollection'/test/:testid'collection :resend'collection here!';member :resend'member here! ' + JSONstringifyreqpathVar;;
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:
routeron'OPTIONS'// resource is the object you routed. You can loop through the methods on it here if you want!reswriteHead200;resend"options, options, options!";;
There is a default HEAD response for any route with a GET, but you can over-ride it like this:
routeron'HEAD'// resource is the object you routed. You can call resource.GET() from here if you want!reswriteHead200;resend"doesn't matter what I type here. node will not send output on a HEAD request";
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:
routeron405// resource is the object you routed. You can loop through the methods on it here if you want!reswriteHead405;resend"this resource doesn't support that method! ";;
There is a default 404 response for any route that doesn't match, but you can over-ride it like this:
routeron404reswriteHead404;resend"nope. couldn't find that!";
(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.)