nestjs如何在请求中获取Cookie?

时间:2018-10-08 15:45:45

标签: nestjs

nestjs如何在请求中获取cookie?

import { Get, Controller, Response, Request } from '@nestjs/common';
import { AppService } from './app.service';

const l = console.log
@Controller()
export class AppController {
  @Get('json')
  json(@Request() req){
    console.log(req.cookies) // undefined
  }
}

2 个答案:

答案 0 :(得分:4)

您必须安装cookie-parser中间件。

$ npm install --save cookie-parser

一旦安装过程完成,只需将中间件绑定到您的应用程序即可:

const app = await NestFactory.create(ApplicationModule);
app.use(cookieParser());

在此处了解更多信息:https://expressjs.com/en/resources/middleware/cookie-parser.html

答案 1 :(得分:0)

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as cookieParser from 'cookie-parser'

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(cookieParser());
  await app.listen(5000);
}
bootstrap();