Skip to content

Instantly share code, notes, and snippets.

@dandean
Created June 8, 2025 23:03
Show Gist options
  • Save dandean/fbf4859d0e35c7b89fa4b165b669dbce to your computer and use it in GitHub Desktop.
Save dandean/fbf4859d0e35c7b89fa4b165b669dbce to your computer and use it in GitHub Desktop.
@Module({
imports: [
// Here we see each of our modules imported and registered:
Module1,
RouterModule.register([{path: 'module-1', module: Module1}]),
Module2,
RouterModule.register([{path: 'module-2', module: Module2}]),
],
})
export class RouteDiscoveryAppModule implements OnApplicationBootstrap {
constructor(
// Inject the global ModuleContainer so we can traverse modules:
private modulesContainer: ModulesContainer,
// Inject Reflector so we can get metadata:
private reflector: Reflector
) {}
onApplicationBootstrap() {
const modules = Array.from(this.modulesContainer.values());
const modulesWithPathPrefix = modules
.map((module) => {
// Use the native `Reflect` API to get all decorator metadata
// on the modules's `class` (eg, metatype):
const metadataKeys = Reflect.getMetadataKeys(module.metatype);
// Look for a metadata key so we can use it to get path metadata:
const pathKey = metadataKeys.find((key) =>
String(key).startsWith('__module_path__')
);
// If we found a path key, pull the path out:
const path = pathKey
? this.reflector.get(pathKey, module.metatype)
: undefined;
return {path, module};
})
// Filter out all modules with no paths:
.filter(({path}) => Boolean(path));
console.log(modulesWithPathPrefix);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment