Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose matched route #223

Closed
kunalgolani opened this issue Dec 29, 2015 · 2 comments
Closed

expose matched route #223

kunalgolani opened this issue Dec 29, 2015 · 2 comments

Comments

@kunalgolani
Copy link

Need to know in the common middlewares for all routes which route will finally be served. Couldn't find a way to do this.

@jergason
Copy link
Collaborator

jergason commented Feb 7, 2016

I'd also like to know what route was matched (separate from the URL on the request) so I could access it in a middleware for monitoring. Say I have some route like this:

route.get('/foo/:id/:type', function*() { this.body = "ok" })

I'd like to be able to create a middleware like this:

function* routeMetrics(next) {
  var start = Date.now()
  yield next
  var duration = Date.now() - start
  // send a metric
  statsd.timing(this.matchedRoute, duration)
  // this.matchedRoute would be the string of the matched route, so '/foo/:id/:type/' in this case
}

route.get('/foo/:id/:type', routeMetrics, function*() { this.body = "ok" })

@kunalgolani
Copy link
Author

if you add another similar but more generic route, this._matchedRoute always gives the more generic route instead of the actually matched more specific one

router.get('/users/:id', function *() {
    // this._matchedRoute is '/:username/:id'
});
router.get('/:username/:id', function *() {
    // this._matchedRoute is '/:username/:id'
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants