/latihan_nestjs

Nest JS Basic Starter

Primary LanguageTypeScript

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript starter repository.

Installation

$ yarn install

Nest CLI

Command Umum : new (bisa disingkat jadi n saja) generate (bisa di singkat jadi g saja) Selengkapnya bisa cek di cli usages

$ nest n namaprojek
$ nest g mo namamodule (otomatis generate module)
$ nest g co namacontroller (otomatis generate controller)
$ nest g s nama service (otomatis generate service)

Setelah kita membuat fungsi/logic di service, kita Inject Service nya ke Controller, contoh :

"getLatihan()"

Maka kita buat juga getLatihan di Controller nya (dengan catatan disini si service yang bakal nge return).

Service Provider

Service provider digunakan agar class yang dimasukan dalam service provifes bisa di inject melalui constructor class lain, tanpa perlu membuat Object.

Service provider berfungsi untuk menampung service-service atau class-class supaya bisa digunakan menggunakan Dependency Injection.

Create API

Kita bisa tambahkan Fungsi Create pada Service dan Controller dengan method POST, lalu panggil dari controller:

task.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class TaskService {
    async createTask(){
        return 'Membuat Tugas baru dari Ayana';
    }
}

Kita pakai fitur async di atas.

ts.controller.ts

import { Controller, Post } from '@nestjs/common';
import { TaskService } from './task.service';

@Controller('task')
export class TaskController {

    constructor(private taskService: TaskService){}

    @Post()
    async createTask(){
        return await this.taskService.createTask;
    }
}

Kita pakai method asyn dan await di atas, lalu pakai Decorator @Post(), lalu bisa di test di Postman untuk method POST nya.

Running the app

# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod

Open Localhost:

localhost:3000/
localhost:3000/hello2
localhost:3000/latihan

Test

# unit tests
$ yarn run test

# e2e tests
$ yarn run test:e2e

# test coverage
$ yarn run test:cov

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.