Export a runnable function
Wraps a function so that it is called when run directly but returned when required in. This is especially useful for micro services and and SOA architectures, but can be used for many other things.
Usage
$ npm install runnable
Basic usage:
// index.js var runnable = ; moduleexports = ; // bar
// bin/foo.js var foo = ; ; // other bar
Usage with multiple runnable instances in one process is a little bit different due to the behavior
of node's module.parent
implementation which sets the parent to the first module to import
the file, as opposed to the actual parent which required it in this time. So to work in this
kind of an enviornment you need to pass a final parameter to the runnable call.
var runnable = ; moduleexports = ;
var runnable = ; moduleexports = ;
var foo = ;var bar = ; ; // foo; // bar
Usage with Browserify
If you are using this module with browserify, it will not work by default. Browserify does
not implement module.parent
or require.main
. Luckily Browserify allows you to provide your
own require implementation via the prelude
option. This module comes with an implementation
which implements the required fields. Hopefully this will get added so Browserify once I make
the case. In the mean time you can do this:
var b = browserify('index.js', {
prelude: require('runnable/browserify-prelude')
}).bundle();
API
;
fnc
: The runnable functiondefaults
: Defaults that will be passed to the function when called directlymodule
: The module the function is declared in, literallymodule
from the commonjs file
Usage in micro-services/SOA
This is an example of how we use this pattern to give production configuration to apps in our SOA setup. In development we just run node index.js
, and in prod we run ./bin/server
which loads production configuration and registers with our service discovery.
// index.js var app = ;var runnable = ; moduleexports = ;
// bin/server var app = ;;