[ ] 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.
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
OnModuleInit should fired without enable HTTP transport
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 {}
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
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.
Most helpful comment
Hi @breitsmiley you're missing to init the app, add this line:
app.init()
before
microservice.listen(()=>{
Regards.