Nest: OnModuleInit is not fired without enabled HTTP transport

Created on 11 Feb 2018  路  3Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


I use NEST application as microservice without HTTP transport layer.
There is some strange behavior of OnModuleInit Lifecycle Event. This event fired only with enabled HTTP transport layer by adding await app.listen(3000); in main.ts

Expected behavior


OnModuleInit should fired without enable HTTP transport

Minimal reproduction of the problem with instructions


For reproduce - just copy content of files below to the src directory of new nestjs project.

Console log without line await app.listen(3000); in main.ts

[Nest] 65 - 2018-2-11 19:28:25 [NestFactory] Starting Nest application...
constructor [AppService]
[Nest] 65 - 2018-2-11 19:28:25 [InstanceLoader] ApplicationModule dependencies initialized +8ms
[Nest] 65 - 2018-2-11 19:28:25 [NestMicroservice] Nest microservice successfully started +3ms
Microservice is listening

As we cant see there isn't log message from onModuleInit. Only message from AppService constructor

Console log with line await app.listen(3000); in main.ts

[Nest] 105 - 2018-2-11 19:32:55 [NestFactory] Starting Nest application...
constructor [AppService]
[Nest] 105 - 2018-2-11 19:32:55 [InstanceLoader] ApplicationModule dependencies initialized +8ms
[Nest] 105 - 2018-2-11 19:32:55 [NestMicroservice] Nest microservice successfully started +3ms
[Nest] 105 - 2018-2-11 19:32:55 [RoutesResolver] App2Controller {/}: +18ms
onModuleInit [AppService]
[Nest] 105 - 2018-2-11 19:32:55 [NestApplication] Nest application successfully started +1ms
Microservice is listening

After enabled WEB server listening onModuleInit starts to fired

main.ts

import {NestFactory} from '@nestjs/core';
import {ApplicationModule} from './app.module';
import {Transport} from '@nestjs/microservices';

async function bootstrap() {
    const app = await NestFactory.create(ApplicationModule);
    const microservice = app.connectMicroservice({
        transport: Transport.REDIS,
        url: 'redis://:redis_pass@c-redis:6379'
    });
    microservice.listen(()=>{
        console.log('Microservice is listening')
    });

   // onModuleInit fired only if you uncomment next line
   // await app.listen(3000);
}
bootstrap();

app.service.ts

import { Component, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
@Component()
export class AppService implements OnModuleInit, OnModuleDestroy {
    constructor() {
      console.log(`constructor [AppService]`);
    }

    onModuleInit() {
        console.log(`onModuleInit [AppService]`);
    }

    onModuleDestroy() {
        console.log(`onModuleDestroy [AppService]`);
    }
}

app.module.ts

import { Module } from '@nestjs/common';
import {AppService} from './app.service';

@Module({
  components: [AppService],
})
export class ApplicationModule {}

What is the motivation / use case for changing the behavior?

Environment


Nest version: 4.5.10


For Tooling issues:
- Node version: v9.4.0  
- Platform:  Linux 

Others:

- node: v9.4.0
- npm: 5.6.0
- Host OS - Ubuntu 16.04 
- Docker Container OS - alpine 3.6.2

Most helpful comment

Hi @breitsmiley you're missing to init the app, add this line:

app.init()

before

microservice.listen(()=>{

Regards.

All 3 comments

Hi @breitsmiley you're missing to init the app, add this line:

app.init()

before

microservice.listen(()=>{

Regards.

@cojack

You're right. I close it. Thanks

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

VRspace4 picture VRspace4  路  3Comments

thohoh picture thohoh  路  3Comments

rlesniak picture rlesniak  路  3Comments

FranciZ picture FranciZ  路  3Comments