0

I've just completed writing my first Node application. It's working well but as I read more from the community and absorb details I've come to the conclusion that my app is a bit different from the examples I'm reading.

So I dug deeper into finding solutions and reading a lot of howtonode.org. I stumbled on the concept of control-flow, promises and the Q-library. I'm starting to digest it all, I think I get it but I wanted to confirm my research and findings. Before I decide to restructure everything with Qlibrary I'm wondering if I've at least got my core app and methodology down pat, it will basically delegate how I move forward at this point.

app.js

// Require global libraries
var app = {
    config     : require('../../config.json'), // my custom config files
    restify    : require('restify'),
    path       : require('path'),
    mongo      : require('mongodb'),
    model      : require('./models.js'),
    q          : require('q'),
    api        : require('./api_util.js'),
    underscore : require('underscore')
};
app.server.pre(function (request, response, next) {
    request.app = app;
    return next();
});
app.server = app.restify.createServer(app.config.api);
require('controller.js')(app);
app.server.listen(app.config.api.port);

controller.js

module.exports = function Controller(app, parcel, controller_name) {
    app.server.get({
        path : '/my/controller/',
        version  : '1.0.0',
        name     : 'My REST GET Controller'
    }, function(request, response, next){
        var model = new request.app.model();
        // do some other stuff
    ));
}

I've taken the approach of encapsulating all my libraries and important components in the app variables and pass it into my controller so that I can call the references directly and only pass in one variable between each callback.

For every request after that I'm accessing the app variable from the request value that I attached to the pre in app.js (hence the request.app.model() function within the route callback function.

I'm wondering if I should be concerned about structuring my code this way. Its made it very easy to pass the reference back and forth without having to worry about global variables or any of that stuff. I read something slightly similar where someone hinted that this is a smart direction because it really helps with namespacing and getting around the whole global variable declaration issue (which is a big no-no).

If I was pushed in the right direction I think Q library and everything else will fall into place rather nicely.

Thanks!

pilotguy
  • 543
  • 1
  • 5
  • 18
  • Don't use a control flow library unless you really need it. If the level of nested callbacks is only 2-3 there's no reason to use such a thing. P.S. Async and Step are the most common control flow libs btw. :) – alessioalex May 24 '12 at 13:28
  • I've been looking into Async, I'll read up on Step right now. My application is far more complex than my samples...so I think a control flow lib is totally necessary. I've got a lot of controllers, multiple support libraries and a bunch of other goodies. Thanks for your feedback! – pilotguy May 24 '12 at 13:38

0 Answers0