express-initializers
An Express App initializer pattern to tame large apps.
Example
Usually your server.js
or app.js
is cluttered with a bunch of app.use
and app.set
middlewares:
var path = exphbs = express = favicon = port = processenvPORT || 3000 app = ; // Set the port for easy accessapp; // Set up favicon servingapp; // Set up the handlebars view enginevar hbs = exphbs;app;rname 'views'));app; /* etc. */ app;
This module aims to let you break each individual middleware configuration into their own file for tidier code. Given an example directory structure like this:
├── app.js├── initializers│ ├── favicon.js│ ├── port.js│ ├── routes.js│ └── views.js
Your app setup file would look something like this:
var express = initialize = app = ; // Let the initializers run ;
And each middleware configuration is moved into its own file. From simple examples like port.js
:
// initializers/port.jsmoduleexports = { app; };
To more complex things like view engines or db initialization:
// initializers/views.jsvar path = exphbs = ; moduleexports = name: 'views' after: 'static' { // Set up the handlebars view engine var hbs = exphbs; app; app; app; };
// initializers/db.jsvar db = Promise = ; moduleexports = { return { // Start the db connection db; }; };
A more thorough implementation can be seen at node-site.
Configuration
The initializers
function returned from require('express-initializers')
can accept options as the second parameter, and an optional callback as the third parameter (if you really hate promises).
;
Initializers
Each individual initializer must be a module that exports an object of the form:
moduleexports = name: 'something' after: 'otherthing' { app; } ;
The name
property can be unique or shared amongst a group of initializers.
The after
property allows you to order your initializers, it signals that this initializer should be ran after another or a group of other initializers.
The configure
method can optionally return a promise for asynchronous configuration.
LICENSE
MIT License, Copyright 2014 Jacob Gable