Moduły, kontrolery i usługi
Poznaj kluczowe komponenty: sposób organizowania kodu przez moduły, obsługi żądań przez kontrolery i enkapsulacji logiki biznesowej przez usługi.
Moduły, kontrolery i usługi to bezpłatna lekcja NestJS Enterprise Backend APIs na CoddyKit. To lekcja 3 z 3. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej NestJS Enterprise Backend APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs NestJS Enterprise Backend APIs zawiera 3 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
NestJS Building Blocks
A NestJS app is built from three core blocks — Modules, Controllers, and Services — that organize code, handle requests, and hold logic.
Organizing with Modules
A Module (class with @Module()) is a feature group: it bundles related controllers and services and defines their dependency scope.
Your First NestJS Module
This AppModule shows the @Module() decorator declaring a controller and a service, making them available within its scope.
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [], // Other modules this module needs
controllers: [AppController], // Controllers handled by this module
providers: [AppService], // Services (providers) available in this module
exports: [], // Providers to export for other modules
})
export class AppModule {}Controllers: Handling Requests
Controllers (classes with @Controller()) handle incoming HTTP requests and return responses, delegating the heavy logic to services.
A Simple API Endpoint
This AppController uses @Get() to handle GET requests at /, calling AppService for the actual message.
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller() // Optional path prefix, e.g., '@Controller('users')'
export class AppController {
constructor(private readonly appService: AppService) {}
@Get() // Handles GET requests to '/' (or '/users' if prefix exists)
getHello(): string {
return this.appService.getHello();
}
}Services: Business Logic Hub
Services (providers marked @Injectable()) hold your business logic and DB calls — reusable units injected into controllers to keep them lean.
Your Logic in a Service
This AppService exposes a method for the controller to call. The @Injectable() decorator lets NestJS manage it via DI.
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello from NestJS!';
}
}Connecting the Dots
Dependency Injection wires it together: declare the parts in a module, ask for the service in the controller's constructor, and Nest injects it.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
console.log('NestJS application is running on http://localhost:3000');
}
bootstrap();
// When you visit http://localhost:3000,
// AppController's getHello() is called,
// which in turn calls AppService's getHello().Component Roles Check
You've learned about Modules, Controllers, and Services. How well do you understand their primary roles?
Core Components Summary
You learned the core trio: Modules organize, Controllers handle requests, Services hold logic — all connected through DI. TypeORM next!
Często zadawane pytania
Czy lekcja „Moduły, kontrolery i usługi” jest bezpłatna?
Tak — pełny tekst „Moduły, kontrolery i usługi” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu NestJS Enterprise Backend APIs, przejdź na CoddyKit PRO. Kurs NestJS Enterprise Backend APIs zawiera 3 lekcji w sumie.
Co nauczysz się w „Moduły, kontrolery i usługi”?
Poznaj kluczowe komponenty: sposób organizowania kodu przez moduły, obsługi żądań przez kontrolery i enkapsulacji logiki biznesowej przez usługi. Ćwiczysz NestJS Enterprise Backend APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć NestJS Enterprise Backend APIs?
Nie wymagamy żadnego doświadczenia. NestJS Enterprise Backend APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 3.
Ile czasu zajmuje lekcja „Moduły, kontrolery i usługi”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji NestJS Enterprise Backend APIs?
Tak. Każda lekcja NestJS Enterprise Backend APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wprowadzenie do frameworka NestJS
- Struktura projektu i CLI
- Moduły, kontrolery i usługi