Hi,
What's the best solution to share "basic" functions between modules?
Is it ok to require a helper module in _every_ module? (because modules are
cached after the first time they are loaded)
e.g.
*// helper.js*
exports.cfg = require("config");
exports.log = new Logger({
...
})
exports.error = function error(childErr) {
var err = new Error();
err.childErr = childErr;
return(err);
}
exports.logError = function logError(err) {
var count = 1;
var errors = []
while(err) {
errors.push("Stack " + count++ + " => " + err + ": " + err.stack);
err = err.hasOwnProperty("childErr") ? err.childErr : undefined;
}
console.log(errors.join("\n"));
return;
}
Requiring helper.js in each module allows application wide log(), cfg and
topass errors around rather than throwing them:
*// model.js*
*var helper = require(helper.js);*
...
if(err) {
return(callback(*helper.error(err)*));
}
...
*// controller.js*
*var helper = require(helper.js);
var log = helper.log;
log.debug("Running controller.js");
*
...
if(err) { // get error from model
*helper.logError(helper.error(err));* // log the error with
all stack traces
return next(new Error("An error occured!")); // return an useful error
message/http status code
}
...
What do you think about this solution? Is it ok to require helper.js in
every module file?
Or is there an easier way? If so, it would be very nice if someone could
post some code examples.
Thanks
Aga
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en