Nest is able to work with different HTTP frameworks (Express by default). Thus, to set a header value that is not known in advance, use API of your chosen HTTP framework. If you are using Express, the code will look like this:
@Post('login')
async login(@Body() body: AuthDto, @Res() res: Response) {
const loginResponse = await this.authService.login(body);
res.header('x-access-token', loginResponse.access_token).json(loginResponse);
}
However, in the given case, you lose compatibility with Nest features that depend on Nest standard response handling, such as Interceptors and @HttpCode() / @Header() decorators. To fix this, you can set the passthrough
option to true:
@Post('login')
async login(@Body() body: AuthDto, @Res({ passthrough: true }) res: Response) {
const loginResponse = await this.authService.login(body);
res.header('x-access-token', loginResponse.access_token);
return loginResponse;
}