Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS controller not mapped

So I have an API that will be deployed in a docker container. This API has the authentications controller, simple and not something special.

When I start up the API in development mode on my local machine, the auth controller will be found and everything is working fine. 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.

Looking into the docker logs, no auth controller will be mapped. Both local and builded docker images should contain the same project files.

auth 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?

like image 588
dewey Avatar asked Jun 18 '20 12:06

dewey


People also ask

How do I set up a new Nestjs controller?

Hint To create a controller using the CLI, simply execute the $ nest g controller cats command. The @Get() HTTP request method decorator before the findAll() method tells Nest to create a handler for a specific endpoint for HTTP requests.

What is @body in Nestjs?

To instruct Nestjs that we need the body values from the request, we should use the @Body() decorator function from the @nestjs/common module before the body parameter. Doing this will bind the body values from the request to the body parameter in the createPost() method.

How do I get parameters in Nestjs?

To get all query parameter values from a GET request, you can use the @Query() decorator function from the @nestjs/common module inside the parameter brackets of the controller's respective method in Nestjs.

How do I post on Nestjs?

To upload a single file, simply tie the FileInterceptor() interceptor to the route handler and extract file from the request using the @UploadedFile() decorator. Hint The FileInterceptor() decorator is exported from the @nestjs/platform-express package. The @UploadedFile() decorator is exported from @nestjs/common .


2 Answers

If you have already tried everything else and nothing worked, try deleting the dist folder. That's what worked for me.

like image 184
Utsav Barnwal Avatar answered Sep 16 '22 14:09

Utsav Barnwal


Did you put the controller in the module?

@Module({
  controllers: [AuthenticationController],
})
export class AppModule {}
like image 33
bashleigh Avatar answered Sep 17 '22 14:09

bashleigh