Tutorial: Overview: libflitter

Overview: libflitter


libflitter is the core package that runs Flitter. Flitter's initialization structure is broken into individual bits called units, and these units are chained one after the other to create the initialization chain (usually referred to as the stack or the chain).

Each unit contains a function that, when called, initializes that particular unit. When forming the stack, Flitter passes each unit's function to the one before it. When it finishes initializing itself, the unit should call that function from within whatever scope it initializes. In Flitter, these are called contexts. The concept of contexts is important to understanding how Flitter works. Each unit of the initialization chain creates a context and the rest of the chain continues from within that context, and therefore has access to the resources that context exposes.

For example, one of the first units to start is the UtilityUnit. This unit creates a helper function, log(), which is bound to UtilityUnit's context. From that point on, all other units that initialize have access to this function via the utility context.

Another example is the DatabaseUnit. The database unit reads the configured model schemata and creates Mongoose models from them. Then, it uses Mongoose to connect to the configured MongoDB connection. It waits for the database to connect before returning the function. That means that all other units loaded after that are guaranteed access to the database -- they run from within (a.k.a. under) the database context.

libflitter contains the units that comprise the core functionality of Flitter, as well as the base classes for things like the FlitterApp and Unit files. In order, libflitter provides the following units:

  • ConfigUnit - parses config files and makes their values available
  • UtilityUnit - various utility functions and info
  • ExpressUnit - configures the underlying Express application
  • ViewEngineUnit - configures the Pug view engine and helper functions
  • DatabaseUnit - creates the database models and connects to MongoDB
  • MiddlewareUnit - loads middleware definitions and makes them available
  • RoutingUnit - register the route definitions with the underlying Express app
  • StaticUnit - make files in the assets/ directory available on the /assets route
  • ErrorUnit - create last-resort handlers for HTTP errors
  • AppUnit - launch the Express HTTP server