Skip to content

Core Concepts

Controllers:

Controllers are responsible for handling incoming requests and returning responses to the client. Controllers define the route handlers that manage the HTTP requests and provide the logic for each endpoint.

To create a controller in NestJS, we use the @Controller() decorator at the class level. This decorator not only marks the class as a controller but also allows us to specify the base route path that this controller will handle.

app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

Controllers define routes and the logic behind responding to different HTTP methods like GET, POST, PUT, DELETE, etc. Inside a controller, each method typically uses a route-specific decorator (e.g., @Get(), @Post(), @Put(), @Delete()) to specify the HTTP method it handles.

Controllers are often injected with services to handle business logic. Dependency Injection (DI) ensures that the controller can access and interact with services or other dependencies.

Controllers can extract dynamic values from the request URL using route parameters with @Param(), query parameters with @Query(), and body data with @Body().

Providers

In NestJS, a provider is any class that can be injected into other classes via the framework’s dependency injection system. Providers are a fundamental part of the application, allowing you to encapsulate business logic, data access, and other services into reusable components, allowing for better modularity and testing. Providers can be services, repositories, factories, helpers, and more.

app.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

Service is decorated with @Injectable(), it can be injected into other parts of the application, like controllers or other providers.

Modules:

Modules are a key organizational element in NestJS applications. They provide a way to group related components, such as controllers, providers, and other modules, into logical and functional subsets. This grouping makes it easier to manage and scale large applications.

A module is defined using the @Module() decorator at the class level. This decorator takes an object that can have properties for imports, controllers, providers, and exports .

main.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
exports: [],
})
export class AppModule {}
  • providers : The providers that will be instantiated by the Nest injector and that may be shared at least across this module

  • controllers : The set of controllers defined in this module which have to be instantiated

  • imports : The list of imported modules that export the providers which are required in this module

  • exports : The exports array is used to export providers, making them available to other modules.

The module encapsulates providers by default. This means that it’s impossible to inject providers that are neither directly part of the current module nor exported from the imported modules. Thus, you may consider the exported providers from a module as the module’s public interface, or API.