NestJS Enterprise Backend APIs · Pelajaran

Modul, Controller, dan Layanan

Dalami komponen inti: cara modul mengatur kode, controller menangani permintaan, dan layanan merangkum logika bisnis.

Pelajaran 3 dari 310 langkah

Modul, Controller, dan Layanan adalah pelajaran NestJS Enterprise Backend APIs gratis di CoddyKit. Ini adalah pelajaran 3 dari 3. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar NestJS Enterprise Backend APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus NestJS Enterprise Backend APIs mencakup 3 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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!

Gratis untuk memulai

Belajar TypeScript dengan tutor AI — gratis

Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.

Kursus
20
Pelajaran
76

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Modul, Controller, dan Layanan” gratis?

Ya — teks lengkap “Modul, Controller, dan Layanan” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus NestJS Enterprise Backend APIs, upgrade ke CoddyKit PRO. Kursus NestJS Enterprise Backend APIs mencakup 3 pelajaran total.

Apa yang akan aku pelajari di “Modul, Controller, dan Layanan”?

Dalami komponen inti: cara modul mengatur kode, controller menangani permintaan, dan layanan merangkum logika bisnis. Kamu berlatih NestJS Enterprise Backend APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai NestJS Enterprise Backend APIs?

Tidak diperlukan pengalaman sebelumnya. NestJS Enterprise Backend APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 3.

Berapa lama pelajaran “Modul, Controller, dan Layanan” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran NestJS Enterprise Backend APIs ini?

Ya. Setiap pelajaran NestJS Enterprise Backend APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Pengenalan Kerangka Kerja NestJS
  2. Struktur Proyek dan CLI
  3. Modul, Controller, dan Layanan
← Kembali ke NestJS Enterprise Backend APIs