NestJS 控制器未映射

dew*_*wey 10 javascript docker typeorm nestjs

所以我有一个将部署在 docker 容器中的 API。这个 API 有authentications控制器,简单而不特别。

当我在本地机器上以开发模式启动 API 时,将找到 auth 控制器并且一切正常。在我的本地机器上构建和运行它也是如此。但是当我对项目进行 dockerize 并在虚拟机上运行它时,我将无法访问 auth 控制器。每个其他控制器都在工作,但 auth 控制器不存在。

查看 docker 日志,不会映射任何身份验证控制器。本地和构建的 docker 镜像都应该包含相同的项目文件。

身份验证控制器:

import {
  Controller,
  Post,
  Delete,
  UseGuards,
  Request,
  Body,
} from '@nestjs/common';

import { AuthenticationsService } from './authentications.service';
import { JwtAuthGuard } from '../shared/guards/jwtAuth.guard';
import { SignInDTO } from './dtos/addGraphNodeToGraphByGraphId.dto';

@Controller('authentications')
export class AuthenticationsController {
  constructor(
    private readonly authenticationsService: AuthenticationsService,
  ) {}

  @Post()
  public signIn(@Body() { username, password }: SignInDTO): Promise<string> {
    return this.authenticationsService.signIn(username, password);
  }

  @Delete()
  @UseGuards(JwtAuthGuard)
  public signOut(@Request() request): Promise<void> {
    return this.authenticationsService.signOut(
      request.encodedToken,
      request.user.tokenExpirationSinceEpochInMilliseconds,
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:

{
    "statusCode": 404,
    "message": "Not Found",
    "error": "Cannot POST /authentications"
}
Run Code Online (Sandbox Code Playgroud)

什么可能导致身份验证控制器不会被映射?

sou*_*rri 7

您是否将控制器放入模块中?

@Module({
  controllers: [AuthenticationController],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)


Uts*_*wal 6

如果您已经尝试了所有其他方法但没有任何效果,请尝试删除该dist文件夹。这就是对我有用的东西。


dew*_*wey 4

最后发现NestJS的一些包有版本6和7。所以它们很可能互相中断。一个指标是大量的警告: 在此输入图像描述

运行后,nest update -f每个控制器都按预期进行了映射。

编辑 :

从 NestJS 版本 9 开始,更新命令已被删除。

请参阅此处:迁移指南 - CLI