tech

Table of Content

Struktur

main.ts

  • Pintu masuk (entry point) aplikasi.
  • Ini adalah file pertama yang dijalankan saat server dimulai.
  • Menginisialisasi NestApplication dan menjalankan server.
  • Contoh:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
 
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT ?? 3000);
}
 
bootstrap();  

app.module.ts

  • Modul utama (root module)
  • Menyimpan semua komponen (controller, service, dll) dalam satu paket.
  • Memastikan komponen bisa saling terhubung.
  • Contoh:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
 
@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
 
export class AppModule {}

app.controller.ts

  • Controller (menerima request dari client)
  • Bertugas menerima permintaan HTTP (seperti GET, POST).
  • Biasanya berisi endpoint API.
  • Contoh:
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();
  }
}

app.service.ts

  • Service (logika bisnis)
  • Tempat menampung logika utama (misal: menghitung, mengambil data dari database, dll).
  • Bisa digunakan oleh banyak controller.
  • Contoh:
import { Injectable } from '@nestjs/common';
 
@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
}

Date: 13-09-2025