固定和NestJS-如何在拦截器中设置响应头 [英] Fastify & NestJS - How to set response headers in interceptor

查看:86
本文介绍了固定和NestJS-如何在拦截器中设置响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在拦截器中设置响应标头,但对于我发现的任何方法都没有运气.我尝试过:

I'm trying to set the response headers in my interceptor, and have had no luck with any method I've found yet. I've tried:

 const request = context.switchToHttp().getRequest();
 const response = context.switchToHttp().getResponse();
 <snippet of code from below>
 return next.handle();

  • request.res.headers ['my-header'] ='xyz'
  • response.header('my-header','xyz')
  • response.headers ['my-header'] ='xyz'
  • response.header ['my-header'] ='xyz'
  • 没有运气.第一个选项表示res未定义,第二个选项无法读取未定义的属性'Symbol(fastify.reply.headers)'",其他选项则什么都不做.

    with no luck. The first option says that res is undefined, the second "Cannot read property 'Symbol(fastify.reply.headers)' of undefined", and the others just do nothing.

    推荐答案

    对于我的 main.ts 中的 FastifyAdapter ,我可以进行以下工作:

    I have the following working for me with the FastifyAdapter in my main.ts:

    @Injectable()
    export class HeaderInterceptor implements NestInterceptor {
      intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        return next.handle().pipe(
          tap(() => {
            const res = context.switchToHttp().getResponse<FastifyReply<ServerResponse>>();
            res.header('foo', 'bar');
          })
        );
      }
    }
    

    使用 .getResponse< FastifyReply< ServerResponse>>()为我们提供正确的输入方式.

    Using .getResponse<FastifyReply<ServerResponse>>() gives us the correct typings to work with.

    @Module({
      imports: [],
      controllers: [AppController],
      providers: [
        AppService,
        {
          provide: APP_INTERCEPTOR,
          useClass: HeaderInterceptor,
        },
      ],
    })
    export class AppModule {}
    

    将拦截器绑定到整个服务器

    Bind the interceptor to the entire server

    ▶ curl http://localhost:3000 -v
    * Rebuilt URL to: http://localhost:3000/
    *   Trying 127.0.0.1...
    * TCP_NODELAY set
    * Connected to localhost (127.0.0.1) port 3000 (#0)
    > GET / HTTP/1.1
    > Host: localhost:3000
    > User-Agent: curl/7.54.0
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < foo: bar
    < content-type: text/plain; charset=utf-8
    < content-length: 12
    < Date: Thu, 14 May 2020 14:09:22 GMT
    < Connection: keep-alive
    < 
    * Connection #0 to host localhost left intact
    Hello World!% 
    

    如您所见,响应以标头 foo:bar 返回,表示拦截器添加了预期的内容.

    As you can see, the response comes back with the header foo: bar meaning the interceptor added what was expected.

    查看您的错误,看来您的第二次尝试可能实际上是 response.headers('my-header','xyz).无论如何,以上内容对我来说都适用于 nest new 应用程序以及最新版本的Nest软件包.

    Looking at your error, it looks like your second attempt may have actually been response.headers('my-header', 'xyz). Whatever the case, the above is working for me on a nest new application, and on the latest version of Nest's packages.

    这篇关于固定和NestJS-如何在拦截器中设置响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆