flatiron / director

a tiny and isomorphic URL router for JavaScript
http://github.com/flatiron/director
MIT License
5.61k stars 486 forks source link

Making route info available #197

Open grownseed opened 11 years ago

grownseed commented 11 years ago

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 like req.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 instance id) would also be very useful. Making this available through the request would be very helpful.

tad-lispy commented 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:

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 :)

polmiro commented 10 years ago

Same here, need to reach out matched pattern, so for a url "/posts/1" retrieve it matched with "/posts/:id/comments"

Nevon commented 9 years ago

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.

vojtatranta commented 9 years ago

+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

vojtatranta commented 9 years ago

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 });