You are on page 1of 2

NESTJS (https://github.com/lujakob/nestjs-realworld-example-app.

git)

Pengenalan
Nestjs merupakan framework nodejs yang digunakan untuk keperluan server-side. Nestjs juga
menggunakan express, tapi menyediakan juga kompatibilitas dengan berbagai library lain.

Instalasi
Nest CLI:

$ npm i -g @nestjs/cli

$ nest new project-name

Controller
Controller bertanggung jawab untuk menangani permintaan yang masuk dan mengembalikan
respons ke klien.

Tujuan controller ini adalah untuk menerima request spesifik untuk aplikasi. Mekanisme routing
mengontrol controller mana yang menerima request. Seringkali, setiap controller memiliki lebih
dari satu route, dan setiap route bisa melakukan aksi yang berbeda.
Untuk membuat basic controller, kita gunakan class dan decorator. Decorator mengaitkan class
dengan metadata yang diperlukan dan memungkinkan Nest untuk membuat routing map
(mengikat request dengan controller yang sesuai)

To create a controller using CLI, simply execute $ nest g controller cats command.

Request payloads
Our previous example of the POST route handler didn't accept any client params. Let's fix this by
adding the @Body() argument here.
But first (if you use TypeScript), we need to determine the DTO (Data Transfer Object) schema.
A DTO is an object that defines how the data will be sent over the network. We could determine
the DTO schema by using TypeScript interfaces, or by simple classes. Surprisingly, we
recommend using classes here. Why? Classes are part of the JavaScript ES6 standard, and
therefore they represent plain functions. On the other hand, since TypeScript interfaces are
removed during the transpilation, Nest can't refer to them. This is important because features
such as Pipes enable additional possibilities when they have access to the metatype of the
variable.

Getting up and running


With the above controller fully prepared, Nest still doesn't know that CatsController exists and as
a result won't create an instance of this class.
Controllers always belong to the module, which is why we hold the controllers array within the
@Module() decorator. Since we don't have any other modules except the root
ApplicationModule, we'll use that to introduce the CatsController

Provider
Modules
Middleware
Exception filters
Pipes
Guards
Interceptors
Custom decorators

You might also like