怎么用nestjs写标题

问题描述 投票:1回答:2

我如何使用nest.js的方式编写标题?

我目前正在使用这个:

import { Controller, Body, Get, Post, HttpCode, HttpStatus, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';
import { AuthService } from './auth.service';
import { Usuario } from '../usuario/usuario.entity';
import { JsonWebTokenError } from 'jsonwebtoken';
import { request } from 'http';

@Controller('auth')
export class AuthController {
    constructor(private readonly authService: AuthService) { }

    @Post('login')
    @HttpCode(HttpStatus.OK)
    async login(@Body('username') username: string, @Body('password') password: string, @Res() response: Response) {
        this.authService
            .validateUser(username, password)
            .then((token) => {
                response.setHeader('Authorization', 'Bearer ' + token);

                let respuesta: any = {};
                respuesta.success = true;
                respuesta.token = token;

                return response.send(respuesta);
            });
    }
}

我不想使用qazxsw poi和qazxsw poi

谢谢你的回答!

header nestjs
2个回答
4
投票

NestJS建立在express之上,所以就像在express中一样:

response.setHeader('Authorization', 'Bearer ' + token);

0
投票

在最新版本中,您可以使用NestJS Core中的return response.send(respuesta);装饰器。

async login(@Body('username') username: string, @Body('password') password: string, @Res() res: Response) {
    const token = await this.authService.validateUser(username, password);
    res.set('Authorization', 'Bearer ' + token);
    res.send({
        success: true,
        token,
    })
});
© www.soinside.com 2019 - 2024. All rights reserved.