Nest:使用 ArgumentHost 和 ExecutionContext 统一处理多种服务类型

Nest.js 的设计允许开发者使用统一的方式处理不同类型的服务,如 HTTP、WebSocket 和基于 TCP 的微服务。

这是通过 ArgumentHostExecutionContext 类实现的,它们提供了跨多种上下文(context)复用 Guards、Interceptors 和 Exception Filters 的能力。

ExecutionContext 类

ExecutionContext 类是一个扩展了 ArgumentsHost 的类。提供了更多关于当前执行过程的信息,比如当前的处理器、类和方法。

ArgumentHost 类

ArgumentHost 是一个包装了当前执行上下文的参数的对象。

我们可以使用 switchToHttp()switchToWs()switchToRpc() 方法来获取特定 HTTP、WebSocket 或 RPC 的上下文信息。
ArgumentHostExecutionContext 的一个超集,它专门用于异常过滤器中。

Exception Filters

创建项目试试:

bash 复制代码
nest new argument-host -p npm

然后创建一个 filter:

bash 复制代码
nest g filter HttpException --flat --no-spec


@Catch() 没有声明参数。表示会 catch 所有类型的异常。

如何声明 Exception Filter 来捕获特定类型的异常呢?

catch 一下 HttpException 异常。

AppModule 全局启用下:

AppController 抛出对应错误:

访问 http://localhost:3000

filter 的第一个参数是异常对象,第二个参数是什么呢?

可以看到,它有这些方法。

host.getType 方法取出当前所处上下文。

host.getArgs 方法就是取出当前上下文的 reqeust、response、next 参数。

调用 host.switchToHttp、host.swtichToWs、host.switchToRpc 方法可以分别切换 http、ws、基于 tcp 的微服务上下文。

这样,就可以在 filter 里处理多个上下文的逻辑了:

访问页面:

guard 那 guard 和 interceptor 里如何使用 context 呢?

创建个 guard:

bash 复制代码
nest g guard role --no-spec --flat

ExecutionContext 扩展了 getClass、getHandler 方法。

这样 Guard、Interceptor 的逻辑可根据目标 class、handler 有没有某些装饰而决定怎么处理。

比如权限验证的时候,我们会先定义几个角色:

自定义装饰器添加 Metadata:

然后在 handler 上添加这个装饰器,参数为 admin:

这样在 Guard 里就可以根据这个 metadata 决定是否放行了:

这里注入的 reflector,并不需要在模块的 provider 声明。

查看页面返回 "Forbidden resource",说明 Guard 生效了,因为 user 是 undefined:

interceptor

在 interceptor 里也有这个 context:

bash 复制代码
nest g interceptor aaa --no-spec --flat

同样可以通过 reflector 取出 class 或者 handler 上的 metdadata。

相关推荐
恋猫de小郭3 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
崔庆才丨静觅9 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606110 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了10 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅10 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅11 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅11 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment11 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅11 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊11 小时前
jwt介绍
前端