-
Notifications
You must be signed in to change notification settings - Fork 16
train(opts)
Bootstraps an express train application, registering files and setting up the application's dependency tree. Note that this returns the unresolved dependency tree. To start the application, you will call .resolve() on the returned tree object.
var train = require('express-train');
var tree = train({base: __dirname, files: '**'});
tree.resolve();returns An nject dependency tree. The application will start when you resolve the tree. For full details, see the documentation on nject.
opts - The configuration object that defines your project structure
opts.base - The base path against which all file patterns will be resolved. It is recommended to use __dirname so that files are resolved relative the the index file that bootstraps the project. If not provided, will default to process.cwd(), which means that the project will resolve differently depending on the cwd at the time the process is invoked.
opts.files - One or more glob patterns representing the files that should be loaded and resolved as part of the project. For full documentation on glob patterns, see node-glob. The file patterns allows for negative globs (!**/*.map). If no positive globs are specified, the file defaults to **. A pattern can also specify an aggregation key to roll up matching files onto a single injectable key. See the examples below for further info.
opts.config - The config directory or file to use for loading of configuration. Defaults to ../config. Can be set to a falsey value to disable configuration. If the path resolves to a directory, the environment-based configuration system is used. If the path resolves to a file, that file is used regardless of environment.
//index.js
module.exports = train({base: __dirname})
Will recursively walk all directories using the ** glob pattern, relative to the index.js file, requiring each file encountered and registering it by name as a dependency.
//index.js
module.exports = train({
base: __dirname
files : '**/*.js'
})
Will require and register only .js files.
//index.js
module.exports = train({
base: __dirname
files : ['{controllers,lib}/**/*.js', 'models/*.js']
})
Recursively walks the controllers and lib directories, registering all js files. Registers all js files that are children of the models directory, but does not recursively walk its subdirectories.
//index.js
module.exports = train({
base: __dirname
files : '!**/*.map'
})
Because there are no positive file patterns, the tree defaults to the ** pattern. Any files which match the negative pattern provided (ending with a .map extension) are skipped.
//index.js
module.exports = train({
base: __dirname
files : ['{controllers,lib}/**/*', 'models/*', '!**/*.map']
})
Loads all files specified by the first two patterns. Any that match the negative pattern, ending in a .map extension, will be skipped.
//index.js
module.exports = train({
base: __dirname
files : ['{controllers,lib}/**', {pattern: 'models/**', aggregateOn: 'models'}]
})
Loads all files from the controllers, lib, and models directory. All files from the models directory are aggregated onto the models key.