Is it safe to rely on Application::_router::stack to get the list of registered routes? #4987
Replies: 2 comments
-
Unfortunately it is a nuanced question, for sure. Officially, there is no documented/supported way to get the list of routes. But the Express.js project treads carefully with changing any data that is accessible, even if it's through an underscore-prefixed property. This is because a large project like this will end up with folks relying on these things and we really don't want to break folks. And, in fact, the router was moved back to a non-underscore-prefixed property in the developing next versions of express, and it's the same object and structure; that should hopefully provide an indication that the underscore part is effectively meaningless :) |
Beta Was this translation helpful? Give feedback.
-
It's awesome news.
Even better. It means that the types library for the next major version will be able to safely type Thanks a lot for the answer. |
Beta Was this translation helpful? Give feedback.
-
I've seen multiple answers on different websites that recommends using
Application::_router::stack
to get the list of registered routes. While it works perfectly, i'm not convinced that it is safe and that it won't break in the future:_router
starts with a_
which usually denotes a private property_router
is of typeany
in the TypeScript definition which may mean that there is no guarantee that it has astack
attributeIs it safe to use this method to get the list of registered routes?
If not, what is the officially recommended way to retrieve such a list?
Thanks by advance.
Beta Was this translation helpful? Give feedback.
All reactions