sholladay / pogo

Server framework for Deno

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[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.

Very cool, thanks for sharing your usage of it.

By the way, I'm working on #62 and those changes will likely fix your bug #66 at the same time. Let me know if you have any other suggestions, as I'm planning a new release soon.

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 an error event that could be globally listened to, like server.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.