-
-
Notifications
You must be signed in to change notification settings - Fork 421
Return the regexp used for matching #329
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
Conversation
While this can be added I’d rather not add it for this reason. I think you’d be better off describing what you actually want as a feature and working on getting that added officially, since that code feels like it could break any time the regex changes and I don’t understand how it would work properly for all the different features of What is it you actually want from Express? |
Hi @blakeembrey, if pillarjs/router#123 will be merged, i will close it.
List of routes. |
Hi @blakeembrey do you plan to fix it in another way, or have you just closed it? |
As I said above:
Since you replied that you want a "list of routes", this isn't the right library for that. The regex isn't helpful toward that goal and you would want to try and land this in the router instead. |
I think it'd also help to be a bit more granular about what a "list of routes" is, or open a PR, or provide some examples in the router library. For example, how should the list handle an array or nested routers? Or a nested router in an array? What's the preferred output format? A flat array, or a nested tree? Do you care about the structure of the application itself in those routes? Should the output format be a string or Most of this isn't something that would be exposed at this level, but features could be added when the above questions get clarification if we need additional features in the library. |
Now I'm creating a list of routes using the modified code by @dougwilson, but function availableRoutes(router) {
const routes = router.stack
.filter(r => r.route)
.map(r => {
return {
method: Object.keys(r.route.methods)[0].toUpperCase(),
path: r.route.path
};
});
const subRouters = router.stack
.filter(r => r.name === "router");
for (const subRouter of subRouters) {
const orig = RegExp.prototype.exec;
let source;
RegExp.prototype.exec = function(...args) {
Object.defineProperty(RegExp.prototype, "exec", {
value: orig,
writable: true,
enumerable: false,
configurable: true
});
source = this.source;
return this.exec(...args);
};
subRouter.matchers[0]();
source = source.replaceAll(/[$?=(){}\\|^]+/g, "").replaceAll(/:[/]+/g, "/");
const subroutes = availableRoutes(subRouter.handle);
for (const sr of subroutes) {
sr.path = `${source}${sr.path}`.replaceAll(/\/+/g, "/");
routes.push(sr);
}
}
return routes;
} to function availableRoutes(router) {
const routes = router.stack
.filter(r => r.route)
.map(r => {
return {
method: Object.keys(r.route.methods)[0].toUpperCase(),
path: r.route.path
};
});
const subRouters = router.stack
.filter(r => r.name === "router");
for (const subRouter of subRouters) {
const source = subRouter.matchers[0].regexp.replaceAll(/[$?=(){}\\|^]+/g, "").replaceAll(/:[/]+/g, "/");
const subroutes = availableRoutes(subRouter.handle);
for (const sr of subroutes) {
sr.path = `${source}${sr.path}`.replaceAll(/\/+/g, "/");
routes.push(sr);
}
}
return routes;
} |
I think I understand what you're doing, I still don't really understand the goals though. I think we might be stuck on the XY problem, where you have a specific solution already but it won't really work in a bunch of different cases or for other users. I'm trying to make assumptions about what you actually want, but I just don't know since I don't have the use-case. For example,
That doesn't seem like what you'd want right? |
This is an attempt to return the functionality to build a routers tree for express app.
Developers can replace
layer.regexp
bylayer.matchers[0].regexp
in original code from @dougwilson expressjs/express#5858 (comment)expressjs/express#5961