Tyboost
Initialization layer and tools for Node.js applications.
Tyboost allows initialization steps to be registered for an application. All these steps will be executed sequentially during startup, after which the application will be ready to run.
Tyboost also provides several helpers to load components such as routes handlers, services with IOC, middlewares, etc.
Table of contents
Installation
$ npm install tyboost
Usage
Tyboost should be applicable to any Node.js application. Currently it has been tested with Express Js which is one of the most common Framework
const express = const tyboost = const config = const app =
This will create an Express Js application with two additional functions: app.register()
and app.boot()
The first one is use to register a step, (manually or with helpers) and the second one is use to boot the application
by executed all registered steps.
The optional config parameter is an object with your own configuration of your application.
It will be available in the IOC container and in your Express JS application by calling app.get('config')
Register step
When starting a Node JS application, some steps must be executed sequentially, such as loading routes, creating services, connecting to databases or registering middleware.
Helpers registration
Tyboost provides several helpers, check the helpers section for more informations
appappapp
Manually registration
You can register manually a function or an array of function
app app
Order of execution
Each step will be executed following the order of registration, BUT ONLY if the step is synchronous
or if the step return a promise.
An async function like setTimeout
will not be execute in the registration flow order
(even it can be execute after boot of application)
// First to be registerapp // Second to be registerapp // Third to be registerapp
Boot application
Call app.boot
to boot your application, this will return a promise.
Registered steps will be executed sequentially, and the promise will be resolve when all steps are complete.
Example with the default promise syntax:
app
Example with await/async syntax:
{ try await app app catcherr console }
Express JS Helpers
Currently the helpers has been design only for Express Js
Routes helper
The routes helpers allows you to load multiple routes from a folder (including the sub folder) at one time. Each file have to return a Express JS router.
You can use this helper with the function tyboost.routes()
Example of a directory structure
.
+-- index.js
+-- src
| +-- handler
| | +-- account
| | | +-- User.js
| | +-- Login.js
File User.js:
let router = expressrouter moduleexports = router
File Login.js:
let router = expressrouter moduleexports = router
To load the routes call the helper like this:
app
You can add prefix /api
a the beginning of your route like this:
app
You can also call this helper with only with one file
app
The routes helper is design to load multiple routes in same time but the following syntax will do the same thing:
let router = express app
Services helper
The services helpers allows you to load multiple services in an IOC (Inversion Of Control) container.
This container will be available in a container parameter of the NodeJS application
and usable for instance in route handler by calling app.get('container')
You have multiple way to call the the service helper:
Load by file
Each file must return an array (or an object) with a service definition, containing the following parameters:
- Unique service name (REQUIRED)
- Definition of Class (REQUIRED)
- Array of dependencies (OPTIONAL, default: [])
- A boolean that define if the service is a singleton (OPTIONAL, default: true)
The values in dependencies object the name of an other service or a config parameters of the main configuration.
Example of a simple service:
const Service = { // Do something } { // Do something } moduleexports = 'service1' Service '@service2' '%param1%' '%section1.param2%' true
or with an object:
moduleexports = name: 'service1' definition: Service dependencies: '@service2' '%param1%' '%section1.param2%' singleton: true
To load the services with a config file:
const express = const tyboost = const config = param1: "value1" section1: param2: "value2" const app = app
After doing this you can use the container in an route like this:
router
Load by object
const Service = {} app
Load by array of object
const Service = {} app
Middleware helper
The routes helpers allows you to load multiple middleware from a folder (including the sub folder) at one time. Each file have to return a function.
If you want that your middleware to be execute before route Handler you have to call the middleware helper before loading route. By the way, if you want to register a middleware that catch all error, you have to call the middleware helper after all the other registration.
Example with two middleware:
appappapp
Example
Repository example are available here: https://github.com/electrotiti/tyboost-example
Tests
To launch the test you just have to do:
$ npm install$ npm test
License
The MIT License
Copyright (c) 2018 Thierry LESZKOWICZ http://github.com/electrotiti