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

add _matchedRoute to context #236

Merged
merged 1 commit into from
Feb 11, 2016
Merged

add _matchedRoute to context #236

merged 1 commit into from
Feb 11, 2016

Conversation

jergason
Copy link
Collaborator

@jergason jergason commented Feb 8, 2016

Expose the route that matched the current request on context. This is useful for monitoring and analytics, and enables you to do something like the following:

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
}

This will send a count of the number of times a specific route is hit, even if the route has dynamic parts.

I'm happy to tweak this, change the name, or add more tests if you are ok with this basic idea.

I believe this fixes #223.

@alexmingoia alexmingoia merged commit b2a89ee into ZijianHe:5.x Feb 11, 2016
this.body = { hello: 'world' };
});

var routerMiddleware = router.routes();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you add another similar but more generic route here, the test will fail for the above route

router.get('/:username/:id', function *() {
    // this._matchedRoute for '/users/:id' will now instead be '/: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

Successfully merging this pull request may close these issues.

3 participants