@Module
export declare let Module: (config: IModuleMetadata) => (Class: any) => any;
Use @Module decorator to declare application module, application must have at least one root module.
Module name must be defined to each module except root module.
IModuleMetadata
export interface IModuleMetadata {
imports?: Array<Function | IProvider>;
exports?: Array<Function | IProvider>;
name?: string;
routes?: Array<RouteRuleConfig>;
controllers?: Array<IProvider | Function>;
providers?: Array<IProvider | Function>;
}
Usage:
@Module({
imports: [AdminModule], // bootstrap in recursive top level order
controllers: [HomeController, CoreController],
providers: [
{
provide: Logger,
useFactory: () => {
let logger: Logger = new Logger();
logger.enable();
logger.printToConsole();
logger.setDebugLevel(LogLevels.BENCHMARK);
return logger;
},
deps: []
},
Router,
Assets,
TemplateEngine,
InMemoryCache
] // providers are initialized in order which thy are defined
})
export class Application implements IAfterConstruct {
@Inject(Assets) assetLoader: Assets;
@Inject(Router) router: Router;
/**
* @function
* @name Application#afterConstruct
*
* @description
* After construct use injected values to define some behavior at entry point
* Defining main route, all routes are processed
*/
afterConstruct() {
this.router.addRules([
{
methods: [Methods.GET],
route: "core/favicon",
url: "/favicon.ico"
},
{
methods: [Methods.GET],
route: "core/assets",
url: "/assets/<file:(.*)>"
},
{
methods: [Methods.GET],
route: "home/id",
url: "/<id:(\\d+)>/<name:(\\w+)>"
},
{
methods: [Methods.GET],
route: "home/id",
url: "/<id:(\\d+)>"
},
{
methods: [Methods.GET],
route: "home/index",
url: "/"
},
{
methods: [Methods.GET],
route: "home/redirect",
url: "/redirect-to-home"
},
{
methods: [Methods.GET],
route: "core/error",
url: "/throw-error"
}
]);
this.router.addRule(DynamicRouteRule);
this.router.setError("core/error");
}
}