【问题标题】:How to set cookies with microservice nest js?如何使用微服务嵌套 js 设置 cookie?
【发布时间】:2022-11-14 07:53:37
【问题描述】:

我有一个身份验证服务和一个 api 网关 所有的 api 请求都通过 api 网关, 问题 : 我无法在身份验证服务中设置 cookie

@post('/signin')
signin(@Body() body:any,@Res({passthrough:true}) res:Response) 
{
return this.appService.signin(body,res);
}

这是我的 api 网关应用程序控制器

signin(body,res)
{
const pattern = { cmd: 'signin' };
return this.clientServiceA.send<any>(pattern,{body,res})
}

这是 api 网关的服务和 这将向身份验证服务 api 发送请求

export class AppController {
@MessagePattern({ cmd: 'signin' })
signin(body,res) {
 res.cookie('jwt',token,{httpOnly:true,expires:new Date(Date.now()+1000*60*60*24*7)});
// i cannot able to use res here it will throw an error
return {
status:200,
message:'login success'
}
}

上面提到的代码用于身份验证控制器。我无法在那里使用 res.cookies。 我如何在此身份验证服务中使用 res.cookies? 任何人都知道请帮助我是微服务架构的新手

【问题讨论】:

    标签: javascript node.js express nestjs microservices


    【解决方案1】:

    您需要使用@Res 装饰器。

    signin(body, @Res() res: Response)
    

    https://docs.nestjs.com/techniques/cookies

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-15
      • 2021-07-06
      • 2018-04-14
      • 2016-10-14
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多