This website works best with JavaScript enabled.
Bookmark
๐Ÿš€ CLI
npm i -g @nestjs/cli
nest new project-name
nest generate module users
nest generate controller users
nest generate service users
nest build
nest start
nest info
๐Ÿ—๏ธ Module
import { Module } from '@nestjs/common';

@Module({
  imports: [],
  controllers: [UsersController],
  providers: [UsersService],
})
export class UsersModule {}
๐ŸŽฎ Controller
import { Controller, Get, Post, Body, Param } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Get()
  findAll() { /* ... */ }

  @Get(':id')
  findOne(@Param('id') id: string) { /* ... */ }

  @Post()
  create(@Body() dto: CreateUserDto) { /* ... */ }
}
โš™๏ธ Service
import { Injectable } from '@nestjs/common';

@Injectable()
export class UsersService {
  findAll() { /* ... */ }
  findOne(id: string) { /* ... */ }
  create(dto: CreateUserDto) { /* ... */ }
}
๐Ÿ›ก๏ธ Provider & Dependency Injection
@Injectable()
export class MyProvider {}

@Module({
  providers: [MyProvider],
})
export class AppModule {}
๐Ÿ—„๏ธ DTO & Validation
import { IsString, IsInt } from 'class-validator';

export class CreateUserDto {
  @IsString()
  name: string;

  @IsInt()
  age: number;
}
๐Ÿ”’ Middleware
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req, res, next) {
    console.log('Request...');
    next();
  }
}
๐Ÿงฉ Pipe
import { PipeTransform, Injectable } from '@nestjs/common';

@Injectable()
export class ParseIntPipe implements PipeTransform {
  transform(value: string) {
    return parseInt(value, 10);
  }
}
๐Ÿ›‘ Exception Filter
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';

@Catch(HttpException)
export class HttpErrorFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    // handle exception
  }
}
๐Ÿƒ Lifecycle Hook
import { OnModuleInit, Injectable } from '@nestjs/common';

@Injectable()
export class AppService implements OnModuleInit {
  onModuleInit() {
    // Called once module is initialized
  }
}
๐Ÿ”— Routing & Params
@Get(':id')
findOne(@Param('id') id: string) { /* ... */ }

@Post()
create(@Body() dto: CreateUserDto) { /* ... */ }
๐Ÿ“ฆ Configuration
npm i @nestjs/config

// app.module.ts
import { ConfigModule } from '@nestjs/config';
@Module({
  imports: [ConfigModule.forRoot()],
})
๐Ÿ—ƒ๏ธ Database (TypeORM Example)
npm i @nestjs/typeorm typeorm sqlite3

// app.module.ts
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
  imports: [TypeOrmModule.forRoot()],
})
๐Ÿงช Testing
npm run test
npm
Mongo
Next

Want to update?

Subscribe to my blog to receive the latest updates from us.

Monthly articles
New articles will be created every month.
No spam
All notifications are notified to you, not spam or advertising.