Node Exporter
Useful modules exporter for global use.
We always need load modules with following codes:
var email = require('../../helpers/email')
If create much more files and directories, I will angry with using the absolute path. So there is a exporter, it is global definition for you.
Load modules:
var $ = require('exporter');
$('email', __dirname + '/email.js');
Use it
var $ = require('exporter');
$.email('hfcorriez@gmail.com');
Install
npm install exporter
Usage
Export
In bootstrap file, such as app.js
in Express.
Export variables
var $ = require('exporter');
$.debug = true;
$.env = process.ENV['NODE_ENV'];
Export modules from file or directory
Project structure such as:
- app.js
- helpers/
- uploader.js
- image.js
- email.js
- tools/
- guid.js
Export
$.uploader = require('./helpers/uploader');
$.email = $(__dirname + '/helpers/email.js');
$('email', __dirname + '/helpers/email.js');
$.helpers = $(__dirname + './helpers');
$('allHelpers', __dirname + '/helpers', true);
Use exporter
In your process file or Express
controller
var $ = require('exporter');
if ($.debug && $.env == 'product') {
}
var guid = $.allHelpers.tools.guid();
var sendEmail = $.helpers.email;
var uploader = $.uploader;
More
In Express
application. You can use to export all routes.
var $ = require('exporter');
$.routes = $(__dirname + '/routes', true);
If you have the structure such as:
- routes/
- register.js
- api/
- users.js
And you can use it simply
var $ = require('exporter');
app.get('/register', $.routes.register);
app.get('/api/users', $.routes.api.users);