简体   繁体   English

NestJS controller 未映射

[英]NestJS controller not mapped

So I have an API that will be deployed in a docker container.所以我有一个 API 将部署在 docker 容器中。 This API has the authentications controller, simple and not something special.这个 API 有authentications controller,简单而不特别。

When I start up the API in development mode on my local machine, the auth controller will be found and everything is working fine.当我在本地机器上以开发模式启动 API 时,将找到 auth controller 并且一切正常。 Same for building and running it on my local machine.在我的本地机器上构建和运行它也是如此。 But when I'll dockerize the project and run it on a virtual machine, then I'll can't access the auth controller. Every other controller is working finde, but the auth controller doesn't exist.但是当我将项目 dockerize 并在虚拟机上运行它时,我将无法访问 auth controller。其他 controller 正在工作,但 auth controller 不存在。

Looking into the docker logs, no auth controller will be mapped.查看 docker 日志,不会映射任何身份验证 controller。 Both local and builded docker images should contain the same project files.本地和构建的 docker 图像应该包含相同的项目文件。

auth controller:授权 controller:

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,
    );
  }
}

Error:错误:

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

What could cause that the authentications controller will not be mapped?什么会导致身份验证 controller 不会被映射?

Did you put the controller in the module?你把controller放在模块里了吗?

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

Finally found out that some packages from NestJS had version 6 and 7. So they propably interrupted each other.最后发现来自 NestJS 的一些包有版本 6 和 7。所以它们可能会相互中断。 An indicator was this flood of warnings:一个指标是大量的警告: 在此处输入图像描述

After running nest update -f every controller was mapped as it was supposed.在运行nest update -f之后,每个 controller 都按照预期进行映射。

If you have already tried everything else and nothing worked, try deleting the dist folder.如果您已经尝试了其他所有方法但没有任何效果,请尝试删除dist文件夹。 That's what worked for me.这对我有用。

I had the same problem as the OP, but it was because I didn't restart my NestJS server after I added that endpoint to the controller.我遇到了与 OP 相同的问题,但这是因为在将该端点添加到 controller 之后,我没有重新启动我的 NestJS 服务器。

Also, I'm used to npm start in other contexts auto-updating the server without restart (React, React Native, I think node?).另外,我习惯于 npm 在其他情况下启动自动更新服务器而无需重新启动(React,React Native,我认为是节点?)。

did you add it to your app module?您是否将其添加到您的应用程序模块中? The Module of auth.身份验证模块。

I also encountered this problem on the server I deployed yesterday.我昨天部署的服务器上也遇到了这个问题。 After a day of research, I was using the master branch, and my code kept pushing to the dev branch, so I hope someone can check that as well.经过一天的研究,我在使用master分支,我的代码一直推送到dev分支,所以我希望有人也能检查一下。

You can also confirm for typo you're missing the @ for @Module() in any of the affected modules usually the app.module.ts您还可以确认您在任何受影响的模块(通常是 app.module.ts)中缺少 @Module() 的拼写错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM