Modules
- modules can be also nested appolo apps with own
injectorand appcontextthey function as independent appolo app. - module can also export instances that will be injected to main app injector.
- modules can load own nested modules.
- each module instance is uniq you can load the same module multiple time with different options.
- modules can be loaded in sequence or in parallel mode.
Module Directory Structure#
module env will be merged with module parent env.
Usage#
each module must inherit from Module
now load module in main app modules
now load modules in parallel
now monitoring controller is part of our app the route /api/monitor in available
Module Config#
module options can be injected in any module class
Module Load#
some time you need to load a module before the main app is loaded for example the logger module
Async Modules#
module can use all appolo features like async factories.
the app will be launched when all modules finished loading
we load the module in the main app and define the id of db client.
now you can inject the db instance anywhere in the main app
Module Lifecycle#
beforeAppInitialize- called on App initializebeforeModuleInitialize- called before module initializebeforeModuleLaunch- called before module launchonInjectInitialize- called on module init methodonInjectBootstrap- called on module bootstrap methodafterModuleInitialize- called after module initialized and inject finish loadingafterModuleLaunch- called after module launched finish the bootstrap processafterAppInitialize- called after app initialized and inject finish loadingafterAppLaunch- called after app launch finish the bootstrap processbeforeReset- called before app resetreset- called before app reset
Dependency injection#
The module instance can be used with inject only after the onInjectInitialize event
Inner modules can inject instances from parent App or modules loaded on the parent App.
it will look the logger instance first in the module injector if not found it will look in the module parent injector util it will reach the root injector if the instance not found error will be thrown
Module Properties#
moduleOptions - return module options
parent- return module parent appapp- return module approotParent- return top root appmoduleOptions- return custom module configdefaults- return default custom module config
Module Function#
Third party modules can be easily loaded into appolo inject and used in inject container.
Each module must call app.module.loadFn before it can be used by appolo launcher.
app.module.loadFn accepts a function as an argument.
The last argument to that function must be the next function, modules loaded serially, so each module must call the next function or return a promise in order to continue the launch process.
Other arguments to the function are object which you wish to inject into the module (these objects must be injected earlier).
By default, each module can inject:
- env - environment object
- inject - injector - to add objects to the injector
- app - the app instance
- or any previous loaded module instances
In config/modules/all.ts
Now we can inject myModuleObject to any class
module can loaded in parallel
A logger module example with winston
In config/modules/all.ts
Now we you inject logger anywhere we need it