Skip to content

Add hook onResponse by route #1828

@RafaelGSS

Description

@RafaelGSS

🚀 Feature Proposal

Based on a conversation in gitter today (at 00h52 AM - São Paulo - Brazil), a user of fastify reported to me that would like create a stopWatch metric for each route.

The fastest solution that I found for her is:

const calcTimeByRoute = routeName => (request, reply) => { 
  console.log(`[${routeName}] Elapsed: ${request.YourDecoratorTimer.elapsedTime()}`)
}

fastify.register(async (fastify, opts) => {
  fastify.get('/route1', async (request, reply) => ({}))
  fastify.addHook('onResponse', calcTimeByRoute('route1'))
})

fastify.register(async (fastify, opts) => {
  fastify.get('/route2', async (request, reply) => ({}))
  fastify.addHook('onResponse', calcTimeByRoute('route2'))
})

But if we expose something like:

fastify.route({
  url: '/route1',
  method: 'GET',
  handler: async (request, reply) => ({}),
  onResponse: (request, reply) => { ... }
}

What do you think? Overhead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions