jeudi 10 septembre 2015

Node.js design pattern

I would like to attempt to build a larger, more modular node.js application than my previous attempts. I am stuck on design patterns as it appears there are way too many ways to accomplish the same thing, and I am lost in the blog FUD around how to accomplish this. I have worked mainly in MVC frameworks where a global app variable is passed around, and I am wondering if this concept is useful in node. For example:

./index.js

var app = {};
var api = require('api'); // Api server module
var db = require('db'); // db client

var dbh = db.connect(); // connect to db
app['dbh'] = dbh;

api.start(app);

./api.js

var API = exports;

API.start = function(app, callback) {
  // do api server stuff
  app['dbh'].execute(... function (err, results){
    console.log('foo');
  });
}

Is this idea of assigning all functions, configuration data, and variables to an app object a reasonable approach?

Aucun commentaire:

Enregistrer un commentaire