Skip to content

Instantly share code, notes, and snippets.

@aizatto
Created October 3, 2017 22:44
Show Gist options
  • Select an option

  • Save aizatto/ab5340fb64dba8d52bbb9b5c878dc9fc to your computer and use it in GitHub Desktop.

Select an option

Save aizatto/ab5340fb64dba8d52bbb9b5c878dc9fc to your computer and use it in GitHub Desktop.
find out routes express sues
'use strict';
const paths = new Set();
const pathsData = {};
const _ = require('lodash');
function getPath(layer) {
if (layer.route && layer.route.path) {
return layer.route.path;
} else if (layer.path) {
return layer.path;
} else if (layer.regexp) {
return `${layer.regexp}`
}
return null;
}
function printStack(stack, prefixes, depth) {
stack.forEach((layer, idx) => {
prefixes.index.push(idx);
prefixes.name.push(layer.name);
const index = prefixes.index.join(':');
const name = prefixes.name.join(':');
let path = getPath(layer);
prefixes.path.push(path);
console.log(`${index}:${name}: ${path} ${layer.regexp}`);
console.log(layer);
if (path) {
const fullpath = prefixes.path.join(',');
if (paths.has(fullpath)) {
pathsData[fullpath].names.push(name);
} else {
pathsData[fullpath] = {
names: [name],
};
paths.add(fullpath);
}
}
const tmp = _.cloneDeep(prefixes);
if (layer.handle && layer.handle.stack) {
printStack(layer.handle.stack, tmp, depth + 1);
}
if (layer.route && layer.route.stack) {
printStack(layer.route.stack, tmp, depth + 1);
}
prefixes.path.pop();
prefixes.name.pop();
prefixes.index.pop();
console.log(``);
});
}
function main() {
var injector = require('./injector')();
injector.bootstrap(function (app) {
// console.log(app._router.stack);
const prefixes = {
name: [],
index: [],
path: [],
};
printStack(app._router.stack, prefixes, 0);
console.log(`Paths: `);
const value = 1;
paths.forEach((path) => {
switch (value) {
case 1:
console.log(`${path}`);
break;
case 2:
console.log(`${path}`);
pathsData[path].names.forEach((name) => {
console.log(` ${name}`);
})
break;
}
});
process.exit(0);
});
}
if (require.main === module) {
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment