0

this is my first question here, so i'd like to apologize in advance if i miss something important.

So this is pretty basic, but i didn't find an answer anywhere, as the title states, my NestJs application running with Fastify, simply does not execute any code after the app.listen('port') line, this doesn't happen with Express. OBS: I'm referring to the main.ts file.

relevant code below:

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({ logger: { level: 'warn' } }),
  );

  const configService = app.get(ConfigService);
  await app.listen(configService.get<number>(ENV_KEYS.PORT), '0.0.0.0');

  console.log(
    `Application is running on: ${await app.getUrl()}\nApplication Version: ${version}`,
  );
}
bootstrap();

The console.log() after await app.listen never executes, even if the app is working normally, and as far as my tests show, no code after await app.listen() ever executes.

I'd like to know how to overcome this issue, because i need to run some code after the app is already bootstrapped.

  • maybe https://github.com/nestjs/nest/issues/7572 – Micael Levi Aug 03 '21 at 22:20
  • Thanks for the reply, actually it was not exaclty the same issue, but it got me on the tracks to find it! Since it does not happen with Express i didn't realized that i was "declaring" the same controllers twice. I'm going to reply my question with the steps i dit to fix it, thanks again @Micael Levi . – Marco Moura Aug 03 '21 at 23:59

1 Answers1

0

So thanks to @Micael Levi pointing me to an issue on github (github.com/nestjs/nest/issues/7572) i started to look into problems within my controllers, and the reason to freeze the application on app.listen() was because of "an double instance" of my AuthController, let me explain

I had my AuthController defined on AuthModule:

// auth.module.ts
@Module({
    imports: [
        PassportModule,
        JwtModule.register({
            secret: JWT_CONSTANTS.SECRET,
        }),
    ],
    controllers: [AuthController],
    providers: [AuthService, LocalStrategy, JwtStrategy],
    exports: [AuthService, LocalStrategy, JwtStrategy],
})

And in the AppModule, i was importing AuthModule while also declaring AuthController AGAIN:

// app.module.ts
@Module({
  imports: [
    ConfigModule.forRoot(getConfigServiceConfiguration()),
    TypeOrmModule.forRootAsync({
      useFactory: async (configService: ConfigService) =>
        getDatabaseModuleOptionsAsync(configService),
      inject: [ConfigService],
    }),
    AuthModule, // <-AuthController is declared within AuthModule scope
    UsuarioModule,
    ClienteModule,
  ],
  controllers: [AppController, AuthController] // <- AuthController here again,
  providers: [AppService],
})

Removing AuthController from controllers:[] in AppModule solved my problem. Rookie mistake, but for some reason this isn't a problem with express, and doesn't raise any compilation errors with Fastify either!