Initialisation
You are reading the documentation for version 2 of FoalTS. Instructions for upgrading to this version are available here. The old documentation can be found here.
In many situations, we need to initialize the application (i.e perform certain actions) before listening to incoming HTTP requests. This is the case, for example, if you need to establish a connection to the database.
There are two ways to achieve this in FoalTS.
#
Initializing the ApplicationThe first approach is to add an init
method to the root controller class which will be called before the application is fully created. This method can be synchronous or asynchronous.
Example 1
#
Initializing a ServiceThe second approach is to add a boot
method in your services. This method can be synchronous or asynchronous.
Example
Boot methods are executed before AppController.init
gets called.
If you manually inject services to your service manager and you want their
boot
methods to be called, you must specify this in theset
method options.
#
Best PracticesIf your initialization consists of several asynchronous tasks, you may want to perform them in parallel. This will reduce the initialization time, which has an impact if you use a serverless architecture.