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

Router events (enabling route documentation) – feature request #4523

Open
mkastner opened this issue Feb 5, 2021 · 3 comments
Open

Router events (enabling route documentation) – feature request #4523

mkastner opened this issue Feb 5, 2021 · 3 comments
Labels

Comments

@mkastner
Copy link

mkastner commented Feb 5, 2021

I am very fond of the express router for it provides a great deal of flexibility. But I have somewhat problems keeping track of all the routes, middleware locations and methods.

It would be great if there was an event implementation that fires up, if e.g. a middleware handler, another route or a method handler is added.

It would then be easy to keep track of the routes and eventually build a documentation.

Since the lines above might not well explain what I have in mind, I have given this some more thought an created a little example module, which is basically a wrapper/decorator for the express router, which sort of mimics and extends the express router behavior.

I hope this example helps explaining why an evented router might be helpful indeed. It's not great code, but rather a quick and dirty hack:

https://github.com/mkastner/mk-express-router-doc

I don't want to be pushing this too far: but if one could add some documentation on the fly to each respective route and path – that would be the icing on the cake.

@getspooky
Copy link
Contributor

getspooky commented Apr 8, 2021

@mkastner This is a good idea, So we will display the domain, method, URI and middleware for the routes it includes in the generated table. The most significant implementation is to add function like debugRoutes It will return the output as follows.

| Domain   |      URI      |  Method |  Middleware |
|-------- -|:-------------:|--------:| -----------:|
|          |  /api         |   GET   | YES         |

@mkastner
Copy link
Author

mkastner commented Apr 8, 2021

@getspooky Thanks for your postive response to my musings on the router.

Yet, I was more leaning toward the idea of the router events being fired only once, when the application starts up. I.e. when the routes are being "mounted".

Like in my example here:

https://github.com/mkastner/mk-express-router-doc/blob/main/test/index.js

If you want the domain, you would have to have an actual request to take place. But then you could simply us a request logger. But that wasn't the concern I was focusing on.

What I had in mind was something like:

const rootRouter = request('express').router();
const childRouter = request('express').router();

// fires e.g. 'method' event -> {route: '/', method: 'get', handler: ['anonymous'|functionName]); 
router.get('/', someHandler);
router.use('/admin', childRouter);
// fires e.g. 'middleware' event -> { parentRoute: '/', route: '/admin', method: '*', handler: ['anonymous',functionName]);

I'd like the events explain the structure of routes:

  • Which route(r) is parent?
    Since each router knows its parent, it's possible to "walk" up the hierarchy to root.
  • What routes are mounted to each router?
  • What's their purpose? Is it a
    • request handler?
    • middleware?
    • router?
  • What's the handler implementation:
    • anonymous function
    • named function

Basically, what I am hoping for is an evented router creation which enables the api programmer to generate a living routing documentation based on the fired informations.

@relaxedrohan
Copy link

Hi this is actually something useful
thanks for the idea @mkastner
i would love to spare my skills on it @getspooky please do let me know what do you think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants