Skip to content

Instantly share code, notes, and snippets.

@lucalves
Created September 12, 2022 12:27
Show Gist options
  • Save lucalves/ae721939041fff62844d49fb39adf26b to your computer and use it in GitHub Desktop.
Save lucalves/ae721939041fff62844d49fb39adf26b to your computer and use it in GitHub Desktop.

Nest.js Cheatsheet

Nest CLI

  • npm i -g @nestjs/cli

Packages

  • yarn add class-validator class-transformer
  • yarn add @nestjs/mapped-types

Commands

  • npm run start:dev / yarn start:dev
  • nest generate controller / nest g co
  • nest generate service / nest g s
  • nest generate modue <name> / nest g mo
  • nest g class address/dto/create-address.dto --no-spec

Decorators

  • @Injectable(): Make a resource injectable via DI
  • @Controller('route'):
  • @Get(), @Post('user/:id/update')
  • @HttpCode(HttpStatus.GONE): Change http status
  • @Param() params: All url params, @Param('id'): Only id param
  • @Query() params: All url params, @Query('id'): Only id param
  • @Body() body: All body values, @Body('id'): Only id value
  • @Res() response: Response object from express
  • @IsNumber(), @IsString(): Validations

Imports

  • import { Body, Controller, Get, Param, Post } from '@nestjs/common';
  • import { IsNumber, IsString } from 'class-validator'; - Validation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment