Tutorial

Tutorial: CRUD REST module in NestJS

January 28, 2026 · Kabim Tech Engineering · 1 min read

← Back to blog
Server room representing backend infrastructure
Server room representing backend infrastructure
NestJS crash course: REST API fundamentals
  • Nestjs
  • Backend

Scaffold a NestJS resource with DTOs, validation pipes, and in-memory storage. The same pattern we use before wiring PostgreSQL.

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

  1. Run `nest new tasks-api` and `cd tasks-api`.
  2. Generate a resource: `nest g resource tasks --no-spec`, choose REST and no Swagger for now.
  3. Define `CreateTaskDto` and `UpdateTaskDto` with `class-validator` decorators (`@IsString`, `@IsOptional`).
  4. Enable `app.useGlobalPipes(new ValidationPipe({ whitelist: true }))` in `main.ts`.
  5. Implement service methods with an in-memory array; use UUIDs from `crypto.randomUUID()`.
  6. 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

  1. Install `@nestjs/typeorm typeorm pg`.
  2. Replace the in-memory array with a `Task` entity and repository.
  3. 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.

Want to talk about your product?

Need a dedicated squad or a fixed-scope MVP? We help teams ship on time.