1.4.2 • Published 6 years ago

eloader v1.4.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Eloader

Just add and run. Load all modules in directory and injects dependencies. Just add your services modules and run parsing the path of the routes.

NEW Added support to ES6 class.

Install

$ npm install eloader

How to

Exists two types of modules: Route and Service.

Service: This module will be injected in routes. Since 1.0 you don't need to register node_modules, obviously you can not use a existent name. Example: fs. In version 1.1 you can load all modules in a directory, but this needs export a $name.

Update In 1.2 you can use $inject in services:

//my service
exports.sample = function() {
	exports.$inject('logger').warn('Something');
}

exports.$inject = {}; //Optional, but need set as empty object.

Routes: The module wich implement your rest api. You need export a main to be executed on load. Optionaly you can export a $inject with all dependency to be injected.

Samples

Loading a directory of routes

Just exports a main. For better perfomance exports $inject as an array with the dependencies.

let myMain = function(app,db) {

	app.get('/route1/add', function (err, data) {
		
		return db.add(data);
	});
}

module.exports.main = myMain;
module.exports.$inject = ['app','db']; //Not required.

Warning: If you swap any dependency in $inject, the main will receive it in this sequence. In the example above app will be db and vice versa.

Loading a directory of modules

Just exports a $name to be used in injection.

let func = function(a,b) { return a+b; };

module.exports.do = func;
module.exports.$name = 'sum'; //Loader will ignore modules without this.

Simple old

const eloader = require('eloader');
const express = require('express');
let app = express();

let options = {debug: false};

eloader.add('options', options)
		.add('options', {}) //Fixed in 1.1. Older overwrite if already exists.
		.add('app', app)
		.run('./loginRoute');

Simple new

//From 1.0
const eloader = require('eloader');
const express = require('express');

let options = {debug: false};

eloader.addObject('options', options) //New methods
		.addServices('services')	  //This will be loaded first.
		.addRoutes('routes')		  //This third
		.run('loginRoute');		  //This second

If you have to load some route first, add this directory in run.

Example: The route which implements login. See the second example.

API

add(name, object)/addObject(name, object) Add an instance of an object.

NameTypeDescription
nameStringName to be used in injection.
objectObjectAn object to be used in injection.

addServices(directory, recursive) Add a directory with modules to be used as service.

NameTypeDescription
directoryStringPath to modules
recursiveBooleanLoad this directory recusively

Warning: Emit error if it is not a directory.

addRoutes(directory, recursive) Add a directory of routes.

NameTypeDescription
directoryStringPath to modules.
recursiveBooleanLoad this directory recusively

Warning: Emit error if it is not a directory. Note: All adds above returns the eloader instance.

on(event, fn) Eloader events.

NameTypeDescription
eventStringEvent name
fnFunctionFunction triggered

run(directory, includeSubDirs) Start to load modules.

NameTypeDescription
directoryStringPath to modules. (Routes)
includeDirsStringIncludes directories in search

Note: Services is loaded first for the injection. Note: The last route directory added will be load first. So if you add a directory in run() this will be executed first.

fallback(path, err) Note Will be removed. path is empty has only err. Use eloader.on('error')

load(path) Load a route

NameTypeDescription
pathStringPath to module.

get(name) Like require, but return objects added by addObject.

NameTypeDescription
nameStringService name.

Events

No more throw, now all thorws above emit an event (error/warn). 1. info - Log when adding. 2. load - After eloader loads all services and routes 3. warn - if something make eloader not work correctly. 4. error - Any error what can stop eloader.

Fallback

NameTypeDescription
errStringThe error.
resObjectObject with stop value.
pathStringIf throws not load a route.

The 'error' like fallback you can return a value to eloader to stop running.

eloader.on('error', (err, res, path) => {

	console.log('[Error]',err);
	res.stop = true; //Will stop eloader
});

Note: Default is false, so its try continue.

Version

1.4.2

Author

Eliézer Augusto de Moraes Andrade

License

MIT

1.4.2

6 years ago

1.3.2

6 years ago

1.2.1

7 years ago

1.1.1

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago