Facility is a configuration component used to configure Plumier application to get a new ability. It consist some middlewares in a correct order, some process before the application initialized and some default application configuration.
For example to build an API you will need: a Body parser, CORS, Generic error handler and some configuration etc, all can be bundled into one facility called
Facility is a class that implements
Facility, the signature of
Facility is like below:
setupcalled during setup process. This method usually used for registering configurations and middlewares
generateRoutescalled during initialization process before the
initializemethod. This method provides list of routes produced by Facility.
initializecalled during initialization process. This method usually used for some preparation required before application run, and possible to call promised functions
Develop your own Facility is not required, you can register middleware and set some configuration manually. You develop Facility if you want to make it reusable.
For example the
WebApiFacility facility is like below:
Above code showing that we setup error handler, body parser and cors with some order. Error handler in the top most, it means it will handle all error caused by the next middleware / action.
In some case if you want to configure Koa, you can do it in facility.