简体   繁体   English

NestJS 在服务中注入请求或执行上下文

[英]NestJS Injecting request or execution context in services

如何在服务中注入请求或执行上下文?

Since version 6.0, current request can be injected into a service, with REQUEST token :从 6.0 版本开始,当前请求可以使用REQUEST令牌注入到服务中:

export class AppService {
  constructor(@Inject(REQUEST) private request) {}

  load() {
    const user = this.request.user;
  }
}

You can inject the request with the @Req() decorator in a controller and then pass the request to a service. 您可以在控制器中使用@Req()装饰器注入请求,然后将请求传递给服务。

Controller 调节器

@Get()
findAll(@Req() request) {
  return this.service.doStuff(request);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM