DiscoveryService, Platform Agnosticism & Plugins
DiscoveryService, Platform Agnosticism & Plugins
Advanced NestJS projects often need framework-like behavior inside the application: find every handler marked with a decorator, register event subscribers automatically, or build a plugin system. DiscoveryService and metadata scanning let you inspect the application graph safely.
Core idea
This feature is about controlling how the application is organized and how it behaves at runtime. These are the points a developer should understand before using it in a real project:
- DiscoveryService exposes wrappers for providers and controllers so you can inspect metadata after Nest builds the container.
- MetadataScanner walks prototype methods and is useful when a decorator is applied to individual methods.
- Platform agnosticism means reusable components should avoid importing Express or Fastify types unless the feature truly depends on one platform.
- Plugins should register providers through modules and metadata, not by manually importing application internals.
- Discovery is a boot-time concern; cache the discovered registry instead of scanning on every request.
Practical example
The following example shows the idea in a practical NestJS project. The goal is not to memorize the snippet, but to understand where it belongs in the architecture:
Production checklist
- Scan once during onModuleInit(), then use an in-memory registry.
- Use explicit metadata decorators for discoverable behavior.
- Avoid platform-specific request objects in reusable plugins.
- Document plugin extension points because discovery makes behavior less obvious than constructor injection.
Summary
This lesson covers an advanced NestJS area that matters when building enterprise applications. Focus on clear boundaries, testable behavior, and choosing the right tool for the context instead of using every feature everywhere.