🐱 NestJS - Node.js TypeScript Backend Framework lấy cảm hứng từ Angular 🅰️ - Có chắc đây là yêu 💜 từ cái nhìn đầu tiên về các nét cơ bản
👋 Vài điều cơ bản về NestJS
- Source code của
NestJS
được viết toàn bộ bằngTypeScript
. Các tín đồ củaTypeScript
đã thấy phấn khích chưa ạ?? 😘
Theo như github source repository thì số lượng star
là >= 38.5k (tại thời điểm 2021/07). Số lượng người follow trên twitter - @nestframework là >= 17.8K. Framework được tạo ra từ năm 2017 bởi @kammysliwiec.
Sử dụng Dependency injection (DI), được truyền cảm hứng từ Dependency injection in Angular. Thông thường được sử dụng để trả về 1 thực thể (instance) đã được truyền vào (inject) đâu đó trong app rồi, từ đó tái sử dụng và dùng chung 1 service.
support:
- GraphQL
- WebSockets
- Microservices
Lazy-loading modules
cho phía backend, giảm thời gian load modules (khác hoàn toàn so vớiLazy-loading modules
của Angular đấy nhé 😄)- OpenAPI - swagger
- TypeORM
- Prisma ORM
👷🏽 Controllers
cats.controller.ts
:
import { Controller, Get } from '@nestjs/common';
@Controller('cats')
export class CatsController {
@Get()
findAll(): string {
return 'This action returns all cats';
}
}
Support:
- Route wildcards
- Status code
- Headers
- Redirection
- Route parameters
- Sub-Domain Routing
- Không đồng Bộ
- Request payloads
- ...
Routing: sử dung @Controller()
decorator để định nghĩa. Với ví dụ trên là @Controller('cats')
.
🧑🔧 Providers
Ý tưởng về phần provider
là nó sẽ được injected
dưới dạng 1 dependency áp dụng nguyên lý DI (Dependency injection) đã nói bên trên.
Các loại provider như: services, repositories, factories, helpers, ...
Ví dụ như cats.service.ts
, dùng để lưu trữ và lấy ra data:
import { Injectable } from '@nestjs/common';
import { Cat } from './interfaces/cat.interface';
@Injectable()
export class CatsService {
private readonly cats: Cat[] = [];
create(cat: Cat) {
this.cats.push(cat);
}
findAll(): Cat[] {
return this.cats;
}
}
interfaces/cat.interface.ts
:
export interface Cat {
name: string;
age: number;
breed: string;
}
Giờ là lúc inject vào CatsController
:
import { Controller, Get, Post, Body } from '@nestjs/common';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';
@Controller('cats')
export class CatsController {
constructor(private catsService: CatsService) {}
@Post()
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
}
@Get()
async findAll(): Promise<Cat[]> {
return this.catsService.findAll();
}
}
🎬 Interceptors
Là chốt chặn giữa clients
và Route Handler
, mục đích là thêm thắt, quản lý response và request.
An interceptor is a class annotated with the
@Injectable()
decorator.
Class annotated
(hay chú thích) @Injectable()
là khái niệm khá quen trong Angular
.
💼 Database
Support SQL hoặc NoSQL database.
Ngoài ra nó còn có các package hỗ trợ làm việc với các ORM (Object–relational mapping]:
🧘♀️ Tổng kết
Ngoài ra còn rất nhiều thứ vân vân và mây mây nữa trong NestJS.
Để làm quen với framework này bạn nhất thiết phải nắm được khái niệm DI.
Điểm mạnh của NestJS
so với các framework khác là:
- Sử dụng
TypeScript
có thể tái sử dụng để check kiểu cho API ởTypeScript
phía Frontend, đặc biệt khi sử dụng thiết kế API - OpenAPI - swagger. Điều này giúp an tâm đồng bộ kiểu trong việc call API, giảm thiểu lỗi về call không đúng cấu trúc API. - Có thể tích hợp nhiều
ORM
khác nhau.
Photo by Trison Thomas on Unsplash 🧘♀️