krakenjs / swaggerize-express

Design-driven apis with swagger 2.0 and express.
Other
354 stars 81 forks source link

Order of creating routes with v5 #136

Closed gonenduk closed 6 years ago

gonenduk commented 6 years ago

With the new version 5, the routes created by this package are created async. As a result, the user of the package is asked to make the server listen only after the routes are ready (an event is triggered). So far - this is simple and easy to implement.

However, in express - the order you create routes is very important. Routes are served by the order they are created. For example, the last route to create is usually an error handler that grabs the entire '/' path and displays a general 404 error. Since routes are created when declared- except the routes created by this package - the routes of this package are always declared the last. As a result - the error route in this example is handled before. We can move the error route declaration to the place where we know the routes are ready - but it's sometimes we use 3rd party packages that create routes as well and that begins to be more complicated.

Do we have a way to solve this? Maybe loading the routes synchronously (with a flag)?

gonenduk commented 6 years ago

What I didn't know (wasn't mentioned in the docs) is that calling swagerrized() creates a new sub app. This may help to solve the issue I described above. Going to check soon and will update about it. Anyway - it is important to note this in the docs!

gonenduk commented 6 years ago

Ok, if I isolate the app created by swagger and add the error handlers at the 'route' event - everything seems ok. I will close the issue.