0Pricing
NestJS Enterprise Backend APIs · Lektion

Module, Controller und Services

Befassen Sie sich mit den Kernkomponenten: wie Module Code organisieren, Controller Anfragen verarbeiten und Services die Geschäftslogik kapseln.

Module, Controller und Services ist eine kostenlose NestJS Enterprise Backend APIs-Lektion auf CoddyKit. Dies ist Lektion 3 von 3. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des NestJS Enterprise Backend APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der NestJS Enterprise Backend APIs-Kurs umfasst insgesamt 3 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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!

Häufig gestellte Fragen

Ist die Lektion „Module, Controller und Services“ kostenlos?

Ja — der vollständige Text von „Module, Controller und Services“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des NestJS Enterprise Backend APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der NestJS Enterprise Backend APIs-Kurs umfasst insgesamt 3 Lektionen.

Was lerne ich in „Module, Controller und Services“?

Befassen Sie sich mit den Kernkomponenten: wie Module Code organisieren, Controller Anfragen verarbeiten und Services die Geschäftslogik kapseln. Du übst NestJS Enterprise Backend APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um NestJS Enterprise Backend APIs zu starten?

Keine Vorkenntnisse erforderlich. NestJS Enterprise Backend APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 3.

Wie lange dauert die Lektion „Module, Controller und Services“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser NestJS Enterprise Backend APIs-Lektion Code schreiben und ausführen?

Ja. Jede NestJS Enterprise Backend APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in das NestJS-Framework
  2. Projektstruktur und CLI
  3. Module, Controller und Services
← Zurück zu NestJS Enterprise Backend APIs