nest中的中间件、守卫、管道、异常过滤器和拦截器

1,832 阅读1分钟

因项目中未使用到中间件和管道,在这里不做描述

数据链路流程图

流程图.jpg

异常过滤器

创建 http-exception.filter.ts 文件

WeChat5665be8f57afc9e0df7acb36841d283b.png

在主文件中使用


app.useGlobalFilters(new HttpExceptionFilter());

统一返回数据

创建 response.interceptor.ts 文件

WeChat7c5396e2f749de731d7cccbddcb93db4.png

在主文件中使用


app.useGlobalInterceptors(new ResponseInterceptor());

验证守卫

创建 auth.guard.ts 文件

WeChat5a5fe5f924e504da8eb86b9d42130a3d.png

在主文件中使用


app.useGlobalGuards(new AuthGuard());