logo
YourData+
玉东数据
首页应用解决方案协作MUI-Remix博客
登录

YourData's blog

NestJs

脚手架

    $ npm i -g @nestjs/cli
    $ nest new project-name 或
    $ nest n my-api
    $ nest g
       class (alias: cl)
       controller (alias: co)
       decorator (alias: d)
       exception (alias: e)
       filter (alias: f)
       gateway (alias: ga)
       guard (alias: gu)
       interceptor (alias: i)
       middleware (alias: mi)
       module (alias: mo)
       pipe (alias: pi)
       provider (alias: pr)
       service (alias: s)

用 pm2 管理进程

pm2 start dist/main.js --watch
# 查询进程
pm2 ls
# 查看日志
pm2 logs

导出项目目录结构

tree ./src/app >tree.md

Request 对象

  @Request() req
  @Response() res
  @Next() next
  @Session() req.session
  @Param(param?: string) req.params / req.params[param]
  @Body(param?: string) req.body / req.body[param]
  @Query(param?: string) req.query / req.query[param]
  @Headers(param?: string) req.headers / req.headers[param]

异常

throw new HttpException({
  status: HttpStatus.FORBIDDEN,
  error: 'This is a custom message',
}, 403);

throw new ForbiddenException();

  • BadRequestException 错误的请求异常 400
  • UnauthorizedException 未经授权的例外情况 401
  • NotFoundException 没有找到例外 404
  • ForbiddenException 禁止的例外 403
  • NotAcceptableException 不可接受的例外 406
  • RequestTimeoutException 请求超时例外 408
  • ConflictException 冲突例外 409
  • GoneException 消除异常 410
  • PayloadTooLargeException 有效负载太大异常 413
  • UnsupportedMediaTypeException 不支持的媒体类型例外 415
  • UnprocessableEntityException 不可处理的异常 422
  • InternalServerErrorException 内部服务器错误异常 500
  • NotImplementedException 未实现的例外 501
  • BadGatewayException 糟糕的网关例外 502
  • ServiceUnavailableException 服务不可用的例外 503
  • GatewayTimeoutException 网关超时例外 504</pre

验证 class-Validation

  • 常见的验证装饰器
    • @IsDefined(value:any)检查是否定义了值(!== undefined,!== null)。这是唯一忽略 skipMissingProperties 选项的装饰器。
    • @IsOptional()检查给定值是否为空(=== null,=== undefined),如果是,则忽略属性上的所有验证器。
    • @Equals(比较:any)检查值是否等于(“===”)比较。
    • @NotEquals(比较:any)检查值是否相等(“!==”)比较。
    • @IsEmpty()检查给定值是否为空(==='',=== null,=== undefined)。
    • @IsNotEmpty()检查给定值是否为空(!=='',!== null,!== undefined)。
    • @IsIn(values:any [])检查 value 是否在允许值数组中。
    • @IsNotIn(values:any [])检查值是否不在不允许值的数组中。
  • 类型验证装饰器
    • @IsBoolean()检查值是否为布尔值。
    • @IsDate()检查值是否为日期。
    • @IsString()检查字符串是否为字符串。
    • @IsNumber(options:IsNumberOptions)检查值是否为数字。
    • @IsInt()检查值是否为整数。
    • @IsArray()检查值是否为数组
    • @IsEnum(entity:object)检查该值是否为有效的枚举
  • 数字验证装饰器
    • @IsDivisibleBy(num:number)检查值是否是可被另一个整除的数字。
    • @IsPositive()检查值是否为正数。
    • @IsNegative()检查值是否为负数。
    • @Min(min:number)检查给定数字是否大于或等于给定数字。
    • @Max(max:number)检查给定数字是否小于或等于给定数字。
  • 日期验证装饰器
    • @MinDate(date:Date)检查值是否是指定日期之后的日期。
    • @MaxDate(date:Date)检查值是否是指定日期之前的日期。
  • 字符串类型验证装饰器
    • @IsBooleanString()检查字符串是否为布尔值(例如“true”或“false”)。
    • @IsDateString()检查字符串是否是日期的完整表示(例如“2017-06-07T14:34:08.700Z”,“2017-06-07T14:34:08.700”或“2017-06-07T14:34: 08 + 04:00" )。
    • @IsNumberString()检查字符串是否为数字。
  • 字符串验证装饰器
    • @Contains(seed:string)检查字符串是否包含种子。
    • @NotContains(seed:string)检查字符串是否包含种子。
    • @IsAlpha()检查字符串是否只包含字母(a-zA-Z)。
    • @IsAlphanumeric()检查字符串是否只包含字母和数字。
    • @IsAscii()检查字符串是否仅包含 ASCII 字符。
    • @IsBase64()检查字符串是否为 base64 编码。
    • @IsByteLength(min:number,max?:number)检查字符串的长度(以字节为单位)是否在一个范围内。
    • @IsCreditCard()检查字符串是否是信用卡。
    • @IsCurrency(options?:IsCurrencyOptions)检查字符串是否是有效的货币金额。
    • @IsEmail(options?:IsEmailOptions)检查字符串是否是电子邮件。
    • @IsFQDN(options?:IsFQDNOptions)检查字符串是否是完全限定的域名(例如 domain.com)。
    • @IsFullWidth()检查字符串是否包含任何全宽字符。
    • @IsHalfWidth()检查字符串是否包含任何半宽字符。
    • @IsVariableWidth()检查字符串是否包含完整和半宽字符的混合。
    • @IsHexColor()检查字符串是否为十六进制颜色。
    • @IsHexadecimal()检查字符串是否为十六进制数。
    • @IsIP(版本?:“4”|“6”)检查字符串是否为 IP(版本 4 或 6)。
    • @IsISBN(版本?:“10”|“13”)检查字符串是否为 ISBN(版本 10 或 13)。
    • @IsISIN()检查字符串是否为 ISIN(库存/安全标识符)。
    • @ IsISO8601()检查字符串是否是有效的 ISO 8601 日期。
    • @IsJSON()检查字符串是否有效 JSON。
    • @IsLowercase()检查字符串是否为小写。
    • @IsMobilePhone(locale:string)检查字符串是否为手机号码。
    • @IsPhoneNumber(region:string)检查字符串是否是有效的电话号码。 “region”接受 2 个字符的大写国家代码(例如 DE,US,CH)。如果用户必须输入 intl。前缀(例如+41),然后您可以传递“ZZ”或 null 作为区域。在 github 上查看 google-libphonenumber,metadata.js:countryCodeToRegionCodeMap
    • @IsMongoId()检查字符串是否是 MongoDB ObjectId 的有效十六进制编码表示。
    • @IsMultibyte()检查字符串是否包含一个或多个多字节字符。
    • @IsNumberString()检查字符串是否为数字。
    • @IsSurrogatePair()检查字符串是否包含任何代理对字符。
    • @IsUrl(options?:IsURLOptions)检查字符串是否为 url。
    • @IsUUID(版本?:“3”|“4”|“5”)检查字符串是否为 UUID(版本 3,4 或 5)。
    • @IsUppercase()检查字符串是否为大写。
    • @Length(min:number,max?:number)检查字符串的长度是否在一个范围内。
    • @MinLength(min:number)检查字符串的长度是否小于给定的数字。
    • @MaxLength(max:number)检查字符串的长度是否不超过给定的数字。
    • @Matches(pattern:RegExp,modifiers?:string)检查字符串是否与模式匹配。匹配('foo',/foo/i)或匹配('foo','foo','i')。
    • @IsMilitaryTime()检查字符串是否是 HH:MM 格式的军事时间的有效表示。
  • 数组验证装饰器
    • @ArrayContains(values:any [])检查数组是否包含给定数组值的所有值。
    • @ArrayNotContains(values:any [])检查数组是否包含任何给定值。
    • @ArrayNotEmpty()检查给定的数组是否为空。
    • @ArrayMinSize(min:number)检查数组的长度是否为最小值。
    • @ArrayMaxSize(max:number)检查数组的长度是否为此数字的最大值。
    • @ArrayUnique()检查所有数组的值是否唯一。对象的比较是基于参考的。
  • 对象验证装饰器
    • @IsInstance(value:any)检查属性是否是传递值的实例。
  • 其他装饰者
    • @Allow()当没有为其指定其他约束时,防止剥离属性。
    • @ValidateNested() 嵌套对象
  • 自定义验证
@ValidatorConstraint()
export class ValDecimalArray implements ValidatorConstraintInterface {
  validate(numberArr: Number[], args: ValidationArguments) {
    return Array.isArray(numberArr)
      && numberArr.filter(i => typeof i === "number" && i >= 0 && i < 1).length === numberArr.length;
  }
  defaultMessage(args: ValidationArguments) {
    return "DecimalArray is >0 <1 number array ";
  }
}
@Validate(ValDecimalArray) value: number[]
孙玉微信号: sunly-

玉东数据是一家专注于互联网应用开发的企业,由从业多年的技术专家组成,在应用开发、网络运维等方面有广泛的经验和实力。

技术涵盖最新的 Web、小程序、Flutter、Android、iOS等多个领域的前沿技术。

商务合作,请微信扫码联系我:

18919890888微信同号
陇ICP备2021001826号