rf-topmodule v0.1.6
rf-topmpdule
- Simple NodeJS module loading lib, that also initializes rf-config and rf-log
- Optional dependency: Uses rf-log for error logging if available
- Optional dependency: Initializes rf-config if available
- Configurable base dir
- Can load a list of modules from a directory, or ALL files in a directory
- Modules are first compiled in a list, and started at a later point together
Getting Started
npm install rf-topmodule
To compile your module list, you have several functions to choose from, for file loading, npm module loading and function loading respectively.
var topModule = require("rf-topmodule");
topModule.init(__dirname);
topModule.setModulePath("myModuleDirectory");
topModule.loadFile("db"); // load myModuleDirectory/db.js
topModule.loadFile("acl", { parameters: 123 }); // provide additional parameters
topModule.loadModule("web", { parameters: 123 }); // load npm module
topModule.loadFunction(function (options, next) {
console.log("I am a function " + options.whatami);
next();
}, { whatami: "without name" } ); // load a function
Instead of using loadFile, loadModule and loadFunction individually, you can also compile the information in a list and pass it to loadModules():
// All of the above could have been in an equivalent list as well:
topModule.loadModules([
"db", // type "file" is assumed implicitly
{ file "acl", type: "file", options: { parameters: 123 } },
{ name: "web", type: "npm", options: { parameters: 123 } },
{ func: function (options, next) {
console.log(options);
next();
}, type: "function", { whatami: "without name" } }
]);
If you intend to load all files in a directory, there's another shortcut function:
topModule.loadModuleDirectory("./modules");
Once you are done preparing your module list, you can start all modules at once:
topModule.startModules();
Module structure
Modules need to provide a 'start' function. The topModule will call it with a list of the remaining module functions and the module parameters.
Note: It is the job of that start function to call the next module. If it fails to do so, the module execution will not finish.
"use strict";
module.exports.start = function (options, next) {
// ...do your module things
// call the next module
next();
}
Accessing modules from other files
Modules can be accessed after they were "loaded" with a special require function. It works the same as the usual require(), but it throws an error if the module hasn't been loaded with the loadModule() et. al. functions previously. The intention is to have a main module that loads and configures all required modules in advance, and receive clear error messages when that approach would otherwise be undermined.
var acl = require("rf-topmodule").require("acl");
// You can now access any module.exports variables and functions from the acl module
Legal Issues
- Licenese: MIT
- Author: Julian von Mendel