'How does nestjs get the cookie in the request?
How does nestjs get the cookie in the request?
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
}
}
Solution 1:[1]
You have to install cookie-parser middleware.
$ npm install --save cookie-parser
once the installation process is completed, simply bind middleware to your application:
const app = await NestFactory.create(ApplicationModule);
app.use(cookieParser());
read more here: https://expressjs.com/en/resources/middleware/cookie-parser.html
Solution 2:[2]
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();
Solution 3:[3]
For everyone, who is looking at this question in 2022.
You can find it in the docs:
https://docs.nestjs.com/techniques/cookies
The other answers don't work, if you use Typescript.
You need to run (according to docs):
$ npm i cookie-parser
$ npm i -D @types/cookie-parser
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kamil Myśliwiec |
| Solution 2 | januw a |
| Solution 3 | NKol |
