[Question] Check if handler already exists for route
KaKi87 opened this issue · comments
Hello,
Is it possible to programmatically check if an handler already exists for a specific route ?
Thanks
You can inspect the existing routes in the routing table by looking at server.router
. In particular, the router has a routes
property and a lookup()
method.
So, for example, you could do something like this:
const route = server.router.lookup('GET', '/');
const hasHandler = Boolean(route?.handler);
Admittedly, the documentation for those APIs is a bit sparse. PRs welcome to expand upon it.
Alright !
Well, I implemented everything I needed to automate in this route.js
file :
- CORS (#31)
- JSON body parsing (#19 / #41)
- params & body validation (#67)
- error catching
(I didn't looked for nor created an issue for this, basically you could expose anerror
event that could be globally listened to, likeserver.on('error', callback)
).
One last thing I'm missing is Sentry (or alternative) integration but that's up to them (getsentry/sentry-javascript#3009).
Oh and I needed BasicAuth too.