FAQ
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • Chad Engler at Sep 14, 2012 at 4:36 pm
    I generally have a "common" module that is included everywhere. I think
    that is fairly common practice.



    -Chad



    From: nodejs@googlegroups.com On Behalf
    Of Aga
    Sent: Friday, September 14, 2012 10:53 AM
    To: nodejs@googlegroups.com
    Subject: [nodejs] Newbie question: How to share basic functions (for
    error handling, logging etc.) between modules?



    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 to pass 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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Richard Rodger at Sep 14, 2012 at 4:54 pm
    Me too - I also put most of my requires in there too.
    Richard


    On 14 Sep 2012, at 17:05, "Chad Engler" wrote:

    I generally have a “common” module that is included everywhere. I think that is fairly common practice.

    -Chad

    From: nodejs@googlegroups.com On Behalf Of Aga
    Sent: Friday, September 14, 2012 10:53 AM
    To: nodejs@googlegroups.com
    Subject: [nodejs] Newbie question: How to share basic functions (for error handling, logging etc.) between modules?

    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 to pass 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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedSep 14, '12 at 3:16p
activeSep 14, '12 at 4:54p
posts3
users3
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase