Building a node app where I'm consistently requiring and doing things with several different bitcoin exchange modules that I've built. Sometimes I want to add or remove an exchange and it's painful to go through the code to add and remove all the hard-coded references to the exchanges.
Is there a way to do something like this? Sorry if this is an obvious question - I'm relatively new to node and a (programming) weekend warrior generally. Thanks in advance for your input.
//config.js
var config = {}
config.exchanges = ['bitfinex', 'bitstamp', 'btce'];
module.exports = config;
//app.js
var config = require('./config');
var _= require('underscore');
_.each(config.exchanges, function(exchange){
//obviously not correct, but how would I accomplish something like this?
var Exchange = require('./api/exchange');
var exchange = new Exchange();
exchange.do_stuff();
});
This i the piece of code that load models, controllers and api files without a reference:
# Bootstrap models
models_path = __dirname + "/models"
fs.readdirSync(models_path).forEach (file) ->
require models_path + "/" + file if ~file.indexOf(".js")
# Bootstrap controllers
controllers_path = __dirname + "/controllers"
fs.readdirSync(controllers_path).forEach (file) ->
app.controllers[file.slice(0, -3)] = require(controllers_path + "/" + file) if ~file.indexOf(".js")
return
# Bootstrap api
api_path = __dirname + "/api"
fs.readdirSync(api_path).forEach (file) ->
app.api[file.slice(0, -3)] = require(api_path + "/" + file) if ~file.indexOf(".js")
return
Hope it helps