Lifecycle Methods

Server Enviroment
Client Enviroment

Overview

Lifecycle methods are methods that are called at different stages of the application lifecycle. They are used to perform actions when the application is bootstrapped, shut down, or when a module is initialized or destroyed.

OnModuleInit

The class that implements the OnModuleInit method is called when the module is initialized.

@Controller()
export class DeliciousMangoController implements OnModuleInit {
    public onModuleInit() {
        console.log('DeliciousMangoModule initialized');
    }
}

@Module({ controllers: [DeliciousMangoController] })
export class DeliciousMangoModule implements OnModuleInit {
    public onModuleInit() {
        console.log('DeliciousMangoModule initialized');
    }
}
typescript

OnModuleDestroy

NOTE

In order to use the `OnModuleDestroy' lifecycle method, the shutdown hook must be enabled in the

.

The class that implements the OnModuleDestroy method is called when the module is destroyed.

@Controller()
export class DeliciousMangoController implements OnModuleDestroy {
    public onModuleDestroy() {
        console.log('DeliciousMangoModule destroyed');
    }
}
typescript

onAppBootstrap

The class that implements the OnAppBootstrap method is called when the application is bootstrapped.

@Controller()
export class DeliciousMangoController implements OnAppBootstrap {
    public onAppBootstrap() {
        console.log('Application bootstrapped');
    }
}
typescript

OnAppShutdown

NOTE

In order to use the `OnModuleDestroy' lifecycle method, the shutdown hook must be enabled in the

.

The class that implements the OnAppShutdown method is called when the application is shut down.

@Controller()
export class DeliciousMangoController implements OnAppShutdown {
    public onAppShutdown() {
        console.log('Application shut down');
    }
}
typescript
Last update at: 2024/06/18 19:03:08