'NestJS proceed with default routing behavior when needed

I have created a wildcard route in my App.controller.ts, in order to catch certain URL requests and handle them. But in case if path contains /API/ - i want to pass handling back to other controllers ( like 'api/user', 'api/something').

Here is my code:

@Get('*/')
  getResources(@Res() res: Response, @Req() req: Request): void {
    if(req.path.startsWith('/api/')){
      // HERE I NEED TO RETURN HANDLING BACK TO 'api/*' controllers
      // Something like next(); - but it doesn't work here - i tried
    }
    // Some other logic
  }

Expected result:

GET <>/banana - // Some other logic GET <>/apple - // Some other logic GET <>/api/user - // Data from api/user controller



Solution 1:[1]

I believe this would work for you

@Controller()
export class MyController {

  @Get('/api') // or /api/*
  async api(): Promise<string> {
    return 'API endpoint';
  }

  @Get('*')
  async other(): Promise<string> {
    return 'Not API'
  }
}

When /api is found it would be handled in api method and everything else will fall back to the other method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 n1md7