Open grownseed opened 11 years ago
:+1:
I am working on access control for routes (to have some routes in my app accessible only for logged in users or admins). For that I am also missing something like @grownseed proposed. In my project I was thinking about adding access_level
property to each method in routing table and before
each dispatch to check that property and stop response if access level is not sufficient. I'd like to be able to do something like this:
app.router.configure before: ->
for path, methods in @req.routes
methods = [ methods ] unless methods instanceof Array
for method in methods
if method.access_level > @req.session.access_level then return @res.end 401, "Not authorized"
The problem is, that there seem to be no easy way to get routes (like @req.routes
in above imaginary example), that will be dispatched for given request.
So my feature request is a method to get route by request, e.g. like that:
@req.routes
orapp.router.getRoutes(@req)
orapp.router.getRoutes(method, path)
(where method
is GET
, POST
, etc. and path
is eg. /documents/123
).It should return object containing matching routes and methods that would be called by dispatch
, eg given GET request for /documents/123
{
"/documents/:id/get": [ function, function ]
}
For recursive routing there would be more properties.
I wish I could contribute some code, but I'm not sure if my comprehension of Director is good enough ATM. Maybe with some guidance I'll try :)
Same here, need to reach out matched pattern, so for a url "/posts/1" retrieve it matched with "/posts/:id/comments"
I would like to echo this feature request. In my statsd middleware, I'd like to tag each metric with the route name, but that information doesn't seem to be available from director.
+1 I needed this thing just a day ago, so I implemented in, in very simple, naive way, but it works in my use case. Did not tested on nested routes. If you want, you can catch out my fork here https://github.com/vojtatranta/director. And to install my fork, run: npm install git+https://github.com/vojtatranta/director.git
And on the instance of router, you just call getInvokedRoute() which will return first matched route, not array of all possibly matched routes.
Don't forget that router instance is this in route handler closure eg. router.on(route, function() { var currentRoute = this.getInvokedRoute(); //your stuff });
When writing middleware, I often find it useful to know what the matched route is, for instance
/something/2
would match/something/:id
. In express, you can do something likereq.route.regexp
, however this property isn't available in Director (not as far as I can tell). Along the same lines, knowing what parameters to look for (in this instanceid
) would also be very useful. Making this available through the request would be very helpful.