Skip to content

Instantly share code, notes, and snippets.

@rnarayana
Created May 12, 2020 07:15
Show Gist options
  • Save rnarayana/d55116bb36c278d360497d9f974395fc to your computer and use it in GitHub Desktop.
Save rnarayana/d55116bb36c278d360497d9f974395fc to your computer and use it in GitHub Desktop.
//From https://github.com/expressjs/express/issues/3308
function getRoutesOfLayer(path: string, layer: any): string[] {
if (layer.method) {
return [layer.method.toUpperCase() + " " + path];
} else if (layer.route) {
return getRoutesOfLayer(
path + split(layer.route.path),
layer.route.stack[0]
);
} else if (layer.name === "router" && layer.handle.stack) {
let routes: string[] = [];
layer.handle.stack.forEach(function (stackItem: any) {
routes = routes.concat(
getRoutesOfLayer(path + split(layer.regexp), stackItem)
);
});
return routes;
}
return [];
}
function split(thing: any): string {
if (typeof thing === "string") {
return thing;
} else if (thing.fast_slash) {
return "";
} else {
var match = thing
.toString()
.replace("\\/?", "")
.replace("(?=\\/|$)", "$")
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//);
return match
? match[1].replace(/\\(.)/g, "$1")
: "<complex:" + thing.toString() + ">";
}
}
function getRoutes(app: Application): string[] {
let routes: string[] = [];
app._router.stack.forEach(function (layer: any) {
routes = routes.concat(getRoutesOfLayer("", layer));
});
return routes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment