I am trying to learn building node api and i want my api to provide configuration settings to the user(like many node does).
So, i tried something like following:
timesheetRoute.js (api file)
// some require() here
var setup = function(options){
winston.info('setup' + options.baseUrl);
rootUrl = options.baseUrl;
// configure the routes here, since rootUrl is available at this time.
app.get(rootUrl + '/:id/view', viewRoute);
app.post('/api/timesheet/:id/post', updateRoute);
};
// some functions here
var customApp = {
'configure': setup,
'work' : app
}
module.exports = customApp;
Now following is the way how i use it in app.js
//some require() call here
var timesheetRoute = require('./routes/timesheetRoute');
// some other function calls, irrelevant to this problem
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
timesheetRoute.configure({baseUrl:'/api/timesheet'});
app.use(timesheetRoute.work);
Problem
Now problem with this type of structure is, my api won't work untill some one has called setup function. What i want is if someone has called setup() then supplied url should be used as rootUrl otherwise a default value like '/api/timesheet' should be used as based url.
putting app.get();
outside the setup()
won't work for obvious reasons, So how to design it in better manner.
Please comment if you need additional information from my side.