Skip to content

NestJS Tutorial

Official Documentation

This tutorial is based on the official NestJS documentation. For the most up-to-date information, visit: https://docs.nestjs.com

Welcome to the comprehensive NestJS tutorial! Learn how to build scalable, maintainable server-side applications with NestJS.

What is NestJS?

NestJS is a progressive Node.js framework for building efficient, reliable, and scalable server-side applications. It uses TypeScript by default and combines elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP).

typescript
// A simple NestJS controller
import { Controller, Get } from '@nestjs/common';

@Controller('hello')
export class HelloController {
    @Get()
    sayHello(): string {
        return 'Hello, NestJS!';
    }
}

Why NestJS?

FeatureDescription
TypeScript FirstBuilt with and fully supports TypeScript
Modular ArchitectureOrganized, maintainable code structure
Dependency InjectionBuilt-in DI container for loose coupling
Extensive EcosystemSupport for databases, GraphQL, WebSockets, microservices
Testing ReadyEasy unit and e2e testing with Jest
Enterprise ReadyScalable architecture for large applications

Tutorial Structure

Beginner Level

Intermediate Level

Advanced Level

Prerequisites

Before starting this tutorial, you should have:

  • Node.js (v16 or higher) installed
  • Basic knowledge of TypeScript
  • Understanding of REST APIs
  • Familiarity with npm/yarn
  • A code editor (VS Code recommended)

Recommended Background

If you're new to TypeScript, check out our TypeScript Tutorial first. Understanding decorators and classes is especially helpful for NestJS.

Quick Start

Installation

bash
# Install NestJS CLI globally
npm install -g @nestjs/cli

# Create a new project
nest new my-project

# Navigate to project
cd my-project

# Start development server
npm run start:dev

Project Structure

my-project/
├── src/
│   ├── app.controller.ts    # Basic controller
│   ├── app.controller.spec.ts # Controller tests
│   ├── app.module.ts        # Root module
│   ├── app.service.ts       # Basic service
│   └── main.ts              # Application entry point
├── test/                    # E2E tests
├── nest-cli.json           # Nest CLI configuration
├── package.json
└── tsconfig.json           # TypeScript configuration

Your First Endpoint

After creating a new project, you already have a working API:

bash
# Start the server
npm run start:dev

# Test the endpoint
curl http://localhost:3000
# Output: Hello World!

Core Concepts Overview

1. Controllers

Handle incoming requests and return responses to the client.

typescript
@Controller('users')
export class UsersController {
    @Get()
    findAll(): string {
        return 'This returns all users';
    }
}

2. Providers (Services)

Handle business logic and can be injected into controllers.

typescript
@Injectable()
export class UsersService {
    findAll(): User[] {
        return this.users;
    }
}

3. Modules

Organize the application into cohesive blocks of functionality.

typescript
@Module({
    controllers: [UsersController],
    providers: [UsersService],
})
export class UsersModule {}

4. Middleware

Functions that have access to request/response objects.

typescript
@Injectable()
export class LoggerMiddleware implements NestMiddleware {
    use(req: Request, res: Response, next: NextFunction) {
        console.log(`Request: ${req.method} ${req.url}`);
        next();
    }
}

NestJS vs Express

AspectExpressNestJS
ArchitectureMinimal, flexibleOpinionated, structured
TypeScriptOptionalFirst-class support
Dependency InjectionManual setupBuilt-in
TestingManual setupBuilt-in support
Learning CurveLowMedium
ScalabilityRequires planningBuilt-in patterns

Common Use Cases

NestJS is excellent for:

  • REST APIs - Full-featured REST API development
  • GraphQL APIs - Built-in GraphQL support
  • Microservices - Native microservice patterns
  • WebSocket Apps - Real-time applications
  • CLI Applications - Command-line tools
  • Scheduled Tasks - Cron jobs and task scheduling

Development Tools

NestJS CLI Commands

bash
# Generate a new module
nest generate module users
# or shorthand
nest g mo users

# Generate a controller
nest g co users

# Generate a service
nest g s users

# Generate a complete CRUD resource
nest g resource users

Useful Extensions (VS Code)

  • NestJS Files - Quick file generation
  • ESLint - Code linting
  • Prettier - Code formatting
  • Thunder Client - API testing

What You'll Build

Throughout this tutorial, you'll build a complete RESTful API with:

  • User authentication (JWT)
  • CRUD operations
  • Database integration
  • Input validation
  • Error handling
  • Unit and E2E tests
  • API documentation

Video Tutorials

Recommended Video Resources

Learn NestJS through these excellent video tutorials from the community.

Free Courses

CourseCreatorDescription
NestJS Crash CourseTraversy Media1.5-hour crash course
NestJS Full TutorialfreeCodeCamp3+ hour comprehensive course
NestJS TutorialAcademind1-hour beginner tutorial
NestJS in 100 SecondsFireshipQuick 100-second explanation

Official Resources

ResourceDescription
NestJS FundamentalsOfficial getting started guide
NestJS CoursesOfficial NestJS courses

Topic-Specific Videos

TopicVideoDuration
AuthenticationNestJS JWT Auth~45 min
Database (TypeORM)NestJS + TypeORM~30 min
GraphQLNestJS GraphQL~40 min
TestingNestJS Testing~35 min

Let's begin your NestJS journey!


Start with Introduction →