NestJS gives structure to Node APIs with modules, controllers, and providers. This tutorial builds a small CRUD API you can swap to a real database later.
What you will build
- A `TasksModule` with controller, service, and DTOs
- `GET /tasks`, `POST /tasks`, `PATCH /tasks/:id`, `DELETE /tasks/:id`
- Validation via `class-validator` and global `ValidationPipe`
Prerequisites
- Node.js 20+
- Nest CLI: `npm i -g @nestjs/cli`
- Basic REST and TypeScript
Step-by-step
- Run `nest new tasks-api` and `cd tasks-api`.
- Generate a resource: `nest g resource tasks --no-spec`, choose REST and no Swagger for now.
- Define `CreateTaskDto` and `UpdateTaskDto` with `class-validator` decorators (`@IsString`, `@IsOptional`).
- Enable `app.useGlobalPipes(new ValidationPipe({ whitelist: true }))` in `main.ts`.
- Implement service methods with an in-memory array; use UUIDs from `crypto.randomUUID()`.
- Test endpoints with curl or the REST Client VS Code extension.
DTO example
import { IsOptional, IsString, MinLength } from "class-validator";
export class CreateTaskDto {
@IsString()
@MinLength(2)
title: string;
@IsOptional()
@IsString()
description?: string;
}Controller routes
@Controller("tasks")
export class TasksController {
constructor(private readonly tasksService: TasksService) {}
@Get()
findAll() {
return this.tasksService.findAll();
}
@Post()
create(@Body() dto: CreateTaskDto) {
return this.tasksService.create(dto);
}
}Add PostgreSQL next
- Install `@nestjs/typeorm typeorm pg`.
- Replace the in-memory array with a `Task` entity and repository.
- Keep DTOs, they still guard the HTTP boundary.
Checklist before production
- Environment-based config (`@nestjs/config`)
- Auth guard on mutating routes
- Structured logging and health check at `GET /health`
You now have the NestJS skeleton Kabim uses on client backend projects.


