NestJS Enterprise Backend APIs · บทเรียน

โมดูล ตัวควบคุม และบริการ

เจาะลึกองค์ประกอบหลัก ได้แก่ วิธีที่โมดูลจัดระเบียบโค้ด ตัวควบคุมจัดการคำขอ และบริการห่อหุ้มตรรกะทางธุรกิจ

บทเรียน 3 จาก 310 ขั้นตอน

โมดูล ตัวควบคุม และบริการ เป็นบทเรียน NestJS Enterprise Backend APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน NestJS Enterprise Backend APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส NestJS Enterprise Backend APIs มีบทเรียนทั้งหมด 3 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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!

เริ่มต้นได้ฟรี

เรียนรู้ TypeScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
20
บทเรียน
76

คำถามที่พบบ่อย

บทเรียน “โมดูล ตัวควบคุม และบริการ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โมดูล ตัวควบคุม และบริการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส NestJS Enterprise Backend APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส NestJS Enterprise Backend APIs มีบทเรียนทั้งหมด 3 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โมดูล ตัวควบคุม และบริการ”

เจาะลึกองค์ประกอบหลัก ได้แก่ วิธีที่โมดูลจัดระเบียบโค้ด ตัวควบคุมจัดการคำขอ และบริการห่อหุ้มตรรกะทางธุรกิจ คุณปฏิบัติ NestJS Enterprise Backend APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน NestJS Enterprise Backend APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน NestJS Enterprise Backend APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน

บทเรียน “โมดูล ตัวควบคุม และบริการ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน NestJS Enterprise Backend APIs นี้ได้ไหม

ได้ บทเรียน NestJS Enterprise Backend APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนะนำเฟรมเวิร์ก NestJS
  2. โครงสร้างโครงการและ CLI
  3. โมดูล ตัวควบคุม และบริการ
← กลับไปที่ NestJS Enterprise Backend APIs