11# Microframework
22
3- Microframework is a simple tool that allows you to execute your modules in a proper order,
3+ Microframework is a simple tool that allows you to execute and configure your modules (called * loaders * ) in a proper order,
44helping you to organize bootstrap code in your application.
55
66## Usage
77
8- First, install the module :
8+ First, install microframework :
99
1010```
1111npm i microframework
1212```
1313
14- Second, create a simple "module" named ` expressModule ` :
14+ Second, create a simple "loader" named ` expressLoader ` .
15+ Loader is a place where you can configure all your modules during microframework bootstrap.
16+ All loaders are executed one by one in a sequential order.
1517
1618``` typescript
1719import {MicroframeworkSettings } from " microframework" ;
1820
19- export function expressModule (settings : MicroframeworkSettings ) {
21+ export function expressLoader (settings : MicroframeworkSettings ) {
2022
2123 // create express app
2224 const app = express ();
@@ -28,69 +30,70 @@ export function expressModule(settings: MicroframeworkSettings) {
2830 // run application to listen on given port
2931 app .listen (3000 );
3032
31- // your module also can return a promise
33+ // your loader also can return a promise and microframework will wait for it in a proper order
3234}
3335```
3436
35- Create ` app.ts ` and bootstrap a microframework and your express module :
37+ Create ` app.ts ` and bootstrap a microframework and your express loader :
3638
3739``` typescript
3840import {bootstrapMicroframework } from " microframework" ;
39- import {expressModule } from " ./expressModule " ;
41+ import {expressLoader } from " ./expressLoader " ;
4042
4143bootstrapMicroframework ([
42- expressModule
44+ expressLoader
4345])
4446 .then (() => console .log (" Application is up and running." ))
4547 .catch (error => console .log (" Application is crashed: " + error ));
4648```
4749
48- That's all. You can do same for other modules .
49- Take a look on sample to understand better how concept of modules and their bootstrapping in microframework.
50+ That's all. You can do same for other loaders .
51+ Take a look on sample to understand better how concept of loaders and their bootstrapping in microframework.
5052
5153## Settings
5254
5355You can specify additional options to microframework.
5456
55- * ` logo ` - Logo needs to be used before application launches. To use logo ansi-art module should be installed.
56- * ` showBootstrapTime ` - If set to true then framework shows how much time was spend to bootstrap all modules .
57- * ` bootstrapTimeout ` - Number of milliseconds to wait before framework will bootstrap all modules .
57+ * ` logo ` - Logo needs to be used before application launches. To use logo ansi-art node module should be installed.
58+ * ` showBootstrapTime ` - If set to true then framework shows how much time was spend to bootstrap microframework .
59+ * ` bootstrapTimeout ` - Number of milliseconds to wait before framework will bootstrap.
5860
5961Example of using settings:
6062
6163``` typescript
6264import {bootstrapMicroframework } from " microframework" ;
65+ import {expressLoader } from " ./expressLoader" ;
6366
6467bootstrapMicroframework ({
6568 config: {
6669 logo: " MyApp" ,
6770 showBootstrapTime: true ,
6871 bootstrapTimeout: 10
6972 },
70- modules : [
71- expressModule ,
73+ loaders : [
74+ expressLoader ,
7275 // ...
7376 ]
7477})
7578 .then (() => console .log (" Application is up and running." ))
7679 .catch (error => console .log (" Application is crashed: " + error ));
7780```
7881
79- ## Sharing data across modules
82+ ## Sharing data across loaders
8083
81- Sometimes few modules need to communicate between each other and use shared data.
82- For such purpose you can store the data in ` settings ` object passed to each module
83- and use stored data across all other modules . For example:
84+ Sometimes few loaders need to communicate between each other and use shared data.
85+ For such purpose you can store the data in ` settings ` object passed to each loader
86+ and use stored data across all other loaders . For example:
8487
8588``` typescript
8689import {MicroframeworkSettings } from " microframework" ;
8790
88- export function expressModule (settings : MicroframeworkSettings ) {
91+ export function expressLoader (settings : MicroframeworkSettings ) {
8992
9093 // create express app
9194 const app = express ();
9295
93- // register all routes, Routes are just routes that should be stored outside of this module
96+ // register all routes, Routes are just routes that should be stored outside of this loader
9497 const routes: any = Routes ;
9598 Object .keys (routes ).forEach (routePath => app .get (routePath , routes [routePath ]));
9699
@@ -101,12 +104,12 @@ export function expressModule(settings: MicroframeworkSettings) {
101104}
102105```
103106
104- And another modules can use data this way:
107+ And another loaders can use data this way:
105108
106109``` typescript
107110import {MicroframeworkSettings } from " microframework" ;
108111
109- export function socketIoModule (settings : MicroframeworkSettings ) {
112+ export function socketIoLoader (settings : MicroframeworkSettings ) {
110113 const io = io ();
111114 io .useExpress (settings .getData (" express_app" ));
112115}
@@ -118,15 +121,16 @@ In the case if you want to shutdown running application you need to do following
118121
119122``` typescript
120123import {bootstrapMicroframework } from " microframework" ;
124+ import {expressLoader } from " ./expressLoader" ;
121125
122126bootstrapMicroframework ({
123127 config: {
124128 logo: " MyApp" ,
125129 showBootstrapTime: true ,
126130 bootstrapTimeout: 10
127131 },
128- modules : [
129- expressModule ,
132+ loaders : [
133+ expressLoader ,
130134 // ...
131135 ]
132136})
@@ -142,7 +146,7 @@ bootstrapMicroframework({
142146 .catch (error => console .log (" Application is crashed: " + error ));
143147```
144148
145- All modules which use resources should release them, for example:
149+ All loaders which use resources should release them, for example:
146150
147151``` typescript
148152export async function typeormModule(settings : MicroframeworkSettings ) {
@@ -156,6 +160,6 @@ export async function typeormModule(settings: MicroframeworkSettings) {
156160 }
157161 });
158162
159- settings .onShutdown (() => connection .close ());
163+ settings .onShutdown (() => connection .close ()); // closing connection on microframework shutdown
160164}
161165```
0 commit comments