当前位置: 首页 > news >正文

平台公司名字大全石家庄网站推广优化

平台公司名字大全,石家庄网站推广优化,做网站开发的女生多吗,设计说明怎么写模板NestJS的Pipe是一个用于数据转换和验证的特殊装饰器。Pipe可以应用于控制器#xff08;Controller#xff09;的处理方法#xff08;Handler#xff09;和中间件#xff08;Middleware#xff09;#xff0c;用于处理传入的数据。它可以用来转换和验证数据#xff0c;确… NestJS的Pipe是一个用于数据转换和验证的特殊装饰器。Pipe可以应用于控制器Controller的处理方法Handler和中间件Middleware用于处理传入的数据。它可以用来转换和验证数据确保数据的有效性和一致性。 内置的 Pipe 是指在使用 NestJS 框架时已经预定义好的一些管道用于验证和转换传入的数据。 ValidationPipe验证管道用于验证传入的数据是否符合指定的规则可以使用 class-validator 和 class-transformer 库来实现验证和转换。ParseIntPipe整数转换管道用于将传入的字符串转换为整数。ParseBoolPipe布尔值转换管道用于将传入的字符串转换为布尔值。ParseArrayPipe数组转换管道用于将传入的字符串转换为数组。ParseUUIDPipeUUID 转换管道用于将传入的字符串转换为 UUID。DefaultValuePipe默认值管道用于在传入的参数不存在时给参数设置一个默认值。ParseEnumPipe枚举转换管道用于将传入的字符串转换为枚举类型。ParseFloatPipe浮点数转换管道用于将传入的字符串转换为浮点数。ParseFilePipe文件转换管道用于将传入的文件转换为上传的文件对象。 接下来我们测试一下内置 Pipe 的功能创建项目 nest new pipe -p npm修改 app.controller.ts 可以看到这个接口默认接收test的字符串参数 import { Controller, Get, Query } from nestjs/common; import { AppService } from ./app.service;Controller() export class AppController {constructor(private readonly appService: AppService) { }Get()getHello(Query(test) test: string): string {return test;} }1、ParseIntPipe 我们可以使用ParseIntPipe 将接收的字符串参数转为整数 import { Controller, Get, ParseIntPipe, Query } from nestjs/common; import { AppService } from ./app.service;Controller() export class AppController {constructor(private readonly appService: AppService) { }Get()getHello(Query(test, ParseIntPipe) test: string): string {return test10;} }访问接口 http://localhost:3000/?test100 可以看到参数test传入 100 变成了 110且当我们传入的参数不能转为init时 则会报错 例如下面 访问 http://localhost:3000/?testww这个返回的报错 我们也可以自定义 但是这种方式需要使用new XxxPipe 的方式 例如指定状态码为404 import { Controller, Get, HttpStatus, ParseIntPipe, Query } from nestjs/common; import { AppService } from ./app.service;Controller() export class AppController {constructor(private readonly appService: AppService) { }Get()getHello(Query(test, new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,})) test: string): string {return test 10;} }同时也可以抛出异常再进行处理 import { Controller, Get, HttpException, HttpStatus, ParseIntPipe, Query } from nestjs/common; import { AppService } from ./app.service;Controller() export class AppController {constructor(private readonly appService: AppService) { }Get()getHello(Query(test, new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException(测试报错 error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test 10;} }2、ParseFloatPipe ParseFloatPipe 的作用是将参数转为float 类型我们新建一个接口 test import { Controller, Get, HttpException, HttpStatus, ParseFloatPipe, ParseIntPipe, Query } from nestjs/common; import { AppService } from ./app.service;Controller() export class AppController {constructor(private readonly appService: AppService) { }Get()getHello(Query(test, new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException(测试报错 error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test 10;}Get(test)getTest(Query(test, ParseFloatPipe) test: number) {return test 10;} }访问 http://localhost:3000/test?test10.4同样可以new ParseFloatPipe 的形式传入 errorHttpStatusCode 和 exceptionFactory 3、ParseBoolPipe 用于将传入的字符串转换为布尔值。 Get(boolean)getBoolean(Query(test) test: boolean) {return test;}访问 http://localhost:3000/boolean?testfalse 4、ParseArrayPipe 用于将传入的字符串转换为数组。 Get(array)getArray(Query(test, ParseArrayPipe) test: number[]) {return test.reduce((pre, cur) pre cur, 0);}保存代码可以发现报错了 提示需要class-validator 这个包class-class-class-validator 允许使用基于装饰器和非装饰器的验证。 内部使用 validator.js 来执行验证。 pnpm install class-validator安装之后启动 发现还是报错 缺少 class-transformerclass-transformer 允许您将普通对象转换为类的某个实例反之亦然。 此外它还允许根据条件序列化/反序列化对象。 这个工具在前端和后端都非常有用。 pnpm install class-transformer 访问 http://localhost:3000/array?test1,2,3,4可以发现 每一项都提取出来了但是类型还是string 没有转换成number类型此时需要 new XxxPipe 的方式传入参数 Get(array)getArray(Query(test, new ParseArrayPipe({items: Number,})) test: number[]) {return test.reduce((pre, cur) pre cur, 0);}再次访问 http://localhost:3000/array?test1,2,3,4同时还可以指定传入时的分割符 separator: ‘/’, Get(array)getArray(Query(test, new ParseArrayPipe({items: Number,separator: /,})) test: number[]) {return test.reduce((pre, cur) pre cur, 0);}访问 http://localhost:3000/array?test1/2/3/4当我们没有传入参数的时候会报错我们可以通过设置 optional 来实现不传参数不报错 Get(array)getArray(Query(test, new ParseArrayPipe({items: Number,separator: /,optional: true,})) test: number[]) {return test}5、ParseEnumPipe 用于将传入的字符串转换为枚举类型。 import { Controller, Get, HttpException, HttpStatus, ParseArrayPipe, ParseEnumPipe, ParseFloatPipe, ParseIntPipe, Query } from nestjs/common; import { AppService } from ./app.service;enum TestEnum {A A111,B B111, }Controller() export class AppController {constructor(private readonly appService: AppService) { }Get()getHello(Query(test, new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException(测试报错 error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test 10;}Get(test)getTest(Query(test, ParseFloatPipe) test: number) {return test 10;}Get(boolean)getBoolean(Query(test) test: boolean) {return test;}Get(array)getArray(Query(test, new ParseArrayPipe({items: Number,separator: /,optional: true,})) test: number[]) {return test}Get(enum/:test)getEnum(Query(test, new ParseEnumPipe(TestEnum)) test: TestEnum) {return test} }访问 http://localhost:3000/enum/111当我们传的参数不是枚举定义的类型时会报错访问 http://localhost:3000/enum/555 6、 ParseUUIDPipe 用于将传入的字符串转换为 UUID。在参数里可以用 ParseUUIDPipe 来校验是否是 UUID首先我们安装一下 uuid 用于生成uuid pnpm install uuid接着我们实现2个接口 getUuid getUuid2 import { Controller, Get, HttpException, HttpStatus, Param, ParseArrayPipe, ParseEnumPipe, ParseFloatPipe, ParseIntPipe, ParseUUIDPipe, Query } from nestjs/common; import { AppService } from ./app.service;const uuid require(uuid);enum TestEnum {A 111,B 222,C 333 }Controller() export class AppController {constructor(private readonly appService: AppService) { }Get()getHello(Query(test, new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException(测试报错 error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test 10;}Get(test)getTest(Query(test, ParseFloatPipe) test: number) {return test 10;}Get(boolean)getBoolean(Query(test) test: boolean) {return test;}Get(array)getArray(Query(test, new ParseArrayPipe({items: Number,separator: /,optional: true,})) test: number[]) {return test}Get(enum/:test)getEnum(Param(test, new ParseEnumPipe(TestEnum)) e: TestEnum) {return e}Get(uuid)getUuid() {return uuid.v4()}Get(uuid/:uuid)getUuid2(Param(uuid, new ParseUUIDPipe) uuid: string) {return uuid} }访问 http://localhost:3000/uuid 获取uuid接着访问 http://localhost:3000/uuid/0a5c5ce9-22ff-4091-85ef-523fcb64223c 可以看到正常访问但是如果参数不是正常的uuid那么会报错http://localhost:3000/uuid/xxx 7、DefaultValuePipe 用于在传入的参数不存在时给参数设置一个默认值。创建一个接口 Get(default)getDefault(Query(test, new DefaultValuePipe(test)) test: string) {return test}当我们没传参数的时候 会使用默认值test http://localhost:3000/default当我们传参数之后将会返回参数 例如 http://localhost:3000/default?test123456 8、自定义Pipe 首先创建一个 pipe: nest g pipe www --flat --no-specwww.pipe.ts: import { ArgumentMetadata, Injectable, PipeTransform } from nestjs/common;Injectable() export class WwwPipe implements PipeTransform {transform(value: any, metadata: ArgumentMetadata) {console.log(value,metadata);return www;} }增加接口 Get(www)getWww(Query(test, WwwPipe) test: string, Query(test2, WwwPipe) test2: string) {return test test2}浏览器访问 http://localhost:3000/www?test1234test21这就是Pipe返回的值控制台打印了 value 就是 query、param 的值而 metadata 里包含 type、metatype、datatype 就是 Query、Param、Body 装饰器或者自定义装饰器metatype 是参数的 ts 类型data 参数值
http://www.w-s-a.com/news/42906/

相关文章:

  • 广东深圳最新情况临安网站seo
  • 华为快速建站女人做春梦网站
  • 建外贸网站费用手机排行榜zol
  • 长治网站制作的网站做网站要什么知识条件
  • discuz 做门户网站wordpress怎么添加图片不显示图片
  • 东营网站建设方案范文百度应用搜索
  • 网站 常见推广js代码放wordpress哪里
  • 靖江网站开发徐州住房和城乡建设局网站
  • 南宁网站建设公司如何为老板打造网站赚钱的wordpress optimizer
  • 做微商好还是开网站好网站网络推广
  • 网站建设岗位所需技能泊头网站优化
  • 企业网站建设是什么网络营销岗位介绍
  • 网站做cdn怎么弄昆明网站seo报价
  • 拖拽网站如何建立微网站
  • 网站网站做代理微信群卖房卡南宁建站模板大全
  • 网络公司怎么优化网站百度快速排名技术培训教程
  • 建e室内设计网 周婷站长工具seo综合查询源码
  • 塔式服务器主机建网站定制美瞳网站建设
  • 网站是先解析后备案吗永久免费网站模板
  • wordpress站点演示php根据ip 跳转网站
  • 东莞市凤岗建设局网站网站开发有哪些职位
  • 企业网站手机版模板免费下载辣条网站建设书
  • 南昌网站建设维护vc 做网站源码
  • 网站动态logo怎么做织梦移动端网站怎么做
  • 三亚城乡建设局网站app下载安装官方网站
  • 公司被其它人拿来做网站郑州哪家做网站最好
  • 山东省建设厅官方网站抖音代运营业务介绍
  • 网站制作 牛商网wordpress商城 微信支付
  • 平面设计培训网站建文帝网站建设
  • python网站建设佛山乐从网站建设