NestJS 控制器未映射

NestJS controller not mapped

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

当我在本地机器上以开发模式启动 API 时,将找到 auth 控制器并且一切正常。在我的本地机器上构建和 运行ning 也是如此。但是当我 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,
    );
  }
}

错误:

{
    "statusCode": 404,
    "message": "Not Found",
    "error": "Cannot POST /authentications"
}

什么会导致无法映射身份验证控制器?

你把控制器放在模块里了吗?

@Module({
  controllers: [AuthenticationController],
})
export class AppModule {}

终于发现 NestJS 的一些包有版本 6 和 7。所以他们可能会互相干扰。一个指标是警告的泛滥:

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

你有没有把它添加到你的应用程序模块中? auth.

的模块

我遇到了与 OP 相同的问题,但这是因为我将该端点添加到控制器后没有重新启动我的 NestJS 服务器。

此外,我习惯于在其他上下文中 npm start auto-updating 服务器而不重启(React,React Native,我认为节点?)。

如果您已经尝试了所有其他方法但均无效,请尝试删除 dist 文件夹。这对我有用。

我昨天部署的服务器上也遇到了这个问题。经过一天的研究,我在使用 master 分支,我的代码一直推送到 dev 分支,所以我希望有人也能检查一下。