Runtim
Dependencies
Say we are making a simple app with an HTTP route (to provide post data). Our app consists of a controller (HTTP endpoint), a provider (logic to access the data) and main module.
- In our controller, we declare the PostsService as a dependency in the constructor
- PostsService is available to be injected in the first place because it has the
@Injectabledecorator.
- In PostsModule, the class
PostsServiceis associated with a token also calledPostsService(providerskey)
- the token is used by the Nestjs runtime to request an instance of a class (most often by the same name. This is configurable, and is what we are doing when we provide the long-form object with keys
provideanduseFactory/useValue,useClass)
@Module({
controllers: [PostsController],
providers: [PostsService],
})