typescript NestJS不Map控制器

1yjd4xko  于 2023-03-31  发布在  TypeScript
关注(0)|答案(2)|浏览(109)

我试图导入app.module.ts上的一些控制器,但在应用程序启动后它们没有被Map。
main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const config = new DocumentBuilder()
    .setTitle('My Gateway')
    .setDescription('API to connect to my services')
    .setVersion('1.0')
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);

  await app.listen(3000);
}
bootstrap();

app.module.ts

@Module({
  imports: [
    OrderModule,
    ProductModule,
    CacheModule.register({ isGlobal: true }),
    ConfigModule.forRoot({ isGlobal: true }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

当我运行npm run start:dev时,除了控制器之外,所有依赖项都被加载

LOG [NestFactory] Starting Nest application...
LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
LOG [InstanceLoader] ProductModule dependencies initialized +0ms
LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
LOG [InstanceLoader] HttpModule dependencies initialized +1ms

ProductModuleOrderModule上,我还导入了控制器

@Module({
  imports: [],
  controllers: [OrderController],
  providers: [OrderService],
  exports: [OrderService],
})
export class OrderModule {}

每个控制器还声明了@Controller装饰器。
如何包含要Map的控制器并开始接受请求?

cwtwac6a

cwtwac6a1#

正如我在评论中提到的你的另一个问题,发生的事情是你的CompanyHttpConfigService依赖于它试图为其创建配置的HttpService,而Nest由于某种原因没有报告循环依赖,即使存在一个,可能是因为在某个地方吞下了一个错误。在那个配置中把HttpService换成raw axios,你应该很好地继续开发。

qni6mghb

qni6mghb2#

原件

您的控制器是否使用de @Controller装饰器标记?

import { Controller, Get } from '@nestjs/common';

@Controller('orders')
export class OrdersController {
  @Get()
  findAll(): string {
    return 'This action returns all orders';
  }
}
编辑1

这是一个在NestJS中复制工作路由的文件代码:

import { NestFactory } from '@nestjs/core';
import { Controller, Get, Injectable, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

@Injectable()
export class OrdersService {
  public doSomething() {
    return [{ foo: 'bar' }];
  }
}

@Controller()
export class OrdersController {
  constructor(private readonly ordersService: OrdersService) {}

  @Get()
  public getOrders() {
    return this.ordersService.doSomething();
  }
}

@Module({
  controllers: [OrdersController],
  providers: [OrdersService],
})
export class OrdersModule {}

@Module({
  imports: [ConfigModule.forRoot(), OrdersModule],
})
export class AppModule {}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

运行npm run start:dev,您应该在终端中看到:

[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [NestFactory] Starting Nest application...
[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [InstanceLoader] AppModule dependencies initialized +7ms
[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [InstanceLoader] OrdersModule dependencies initialized +0ms
[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [RoutesResolver] OrdersController {/}: +5ms
[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [RouterExplorer] Mapped {/, GET} route +2ms
[Nest] 63969  - 03/29/2023, 10:55:54 PM     LOG [NestApplication] Nest application successfully started +2ms

相关问题