express icon indicating copy to clipboard operation
express copied to clipboard

Router events (enabling route documentation) – feature request

Open mkastner opened this issue 3 years ago • 3 comments

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.

mkastner avatar Feb 05 '21 09:02 mkastner

@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         |

getspooky avatar Apr 08 '21 09:04 getspooky

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

mkastner avatar Apr 08 '21 10:04 mkastner

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

relaxedrohan avatar Jun 02 '22 18:06 relaxedrohan