Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple paths for the same handler in fastify

Tags:

fastify

In fastify, is there a way I can add an alias to a route/ path? Basically, having two paths be handled the same way. This is useful for migrating an APIs.

I understand that I can abstract the handler function into a named function, and pass that function to both routes. I am just wondering if there's another way to do so?

like image 907
Tri Nguyen Avatar asked Sep 16 '25 05:09

Tri Nguyen


1 Answers

No, there is not the feature to set an array of routes to one handler (here the registration logic if you would like to add this feature).

I would suggest registering routes like this:

['/', '/alias'].forEach(path => {
  fastify.route({
    method: ['GET'], // you could define multiple methods
    url: path,
    handler: mySharedHandler
  })
})
like image 148
Manuel Spigolon Avatar answered Sep 17 '25 20:09

Manuel Spigolon