bi-service-template plugin should help you get running. It generates working project skeleton based on minimal user input.
Here is basic project file system structure:
config/
config/<NODE_ENV>/config.json5
- service configuration
lib/
lib/routes/
- by convention, not required
- contents of the directory will be recursively loaded (by user-defined rules) at service startup therefore filenames don't have to match any pattern.
lib/routes/v1.0/
lib/routes/v1.0/<app_name>/
lib/app.js
- by convention registers individual applications running within the service and exports the
AppManager
instance - A service can have multiple applications running on different ports.
- by convention registers individual applications running within the service and exports the
logs/
- when logging into the fs
index.js
- is REQUIRED to export the
Service
instance
- is REQUIRED to export the
A service can be run by npm start
or by ./node_modules/.bin/bi-service run
command respectively.
See CLI Interface for list of options.
A Service
instance holds the main representation of a running web service. It's aware of AppManager
with Apps
which each may listen on different TCP port. Also the service object has access to essential ResourceManager
& RemoteServiceManager
& Service Config
require('bi-service')
=== Service Constructor
$PROJECT_ROOT/index.js
index.js
is the entry point of a service and the only file with enforced purpose/structure. It's required to:
- export
Service
instance object - register external resources (database connections, dependent services etc..) needed by the service
- it's responsible for attaching
Service#event:set-up
event listener which should initialize individual applications running within the service - if any optional plugins are desirable, they should be loaded at bottom of the file by simply requiring (
require('bi-service-<pluginname>')
) the package.
example:
const config = require('bi-config');
const Service = require('bi-service').Service;
const sequelize = require('./lib/database/sequelize.js');
const service = module.exports = new Service(config);
const resourceMgr = service.resourceManager;
//
resourceMgr.register('postgres', sequelize);
service.on('set-up', function() {
//initialize applications with route definitions
require('./lib/app.js');
});
// bi-service plugin registration
require('bi-service-doc');
service.ShellApp = require('bi-service-shell').App;
See Service#event:set-up
event API.
$PROJECT_ROOT/lib/app.js
the destination and filename is purely conventional matter and can be placed in custom location.
It initializes all applications
which will be run within the service.
const moduleLoader = require('bi-service').moduleLoader;
const service = require('../index.js');
module.exports = service.appManager;
//creates a http application
service.buildApp('app-name', {
//constructor options
}).once('post-init', function() {
//custom initialization stuff
});
//creates a cli application
//accessible through ./node_modules/.bin/bi-service executable
service.buildApp('app-name', {
//constructor options
}, service.ShellApp);
//recursively `require`s `*.js` modules
moduleLoader.loadModules([
__dirname + '/routes/v1.0/', //versioned http endpoints
__dirname + '/routes/shell/', //user defined shell commands
], {
except: [] //list of paths that should NOT be imported
});
See the API of AppInterface
application interface.
See Routing tutorial for a basic endpoint definition.