expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.57k stars 16.15k forks source link

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

Open mkastner opened 3 years ago

mkastner commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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

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 commented 2 years ago

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