init-system
Declarative startup/shutdown for your Node.js apps.
This solution has a dependency declarations similar to Linux startup systems or Makefiles.
It also has inversion of control similar to middleware.
Usage
First we need to declare our services.
module.exports = {
provides: 'myService',
after: ['config', 'database'],
service: function*(services, provide) {
const myService = new MyService(services.config, services.database);
const exitCode = yield provide(myService);
myService.close();
return exitCode;
}
};
Then in our main script, load them using init-system
.
const init = require('init-system');
init.services(
[
require('./services/config.js'),
require('./services/my-service.js'),
require('./services/database.js')
],
function(err, exitCode){
if(err){
console.error(`Error during application startup / shutdown: ${err}`);
} else {
console.log(`Application exitted with ${exitCode}`);
}
}
);
Available options
You can call init.services(services, [options], callback)
with an options object to change some behavior.
key | type | description |
---|
logger | function(message) | Allows you to set a logger for verbose startup / shutdown messages. |
View example usage for the options
const options = {
logger: function(message) {
console.log('init-system', message);
}
};
init.services(
[],
options,
function(err, exitCode) { }
);