json-routing-v-ks
Version:
Extended version of Giorgio Modoni JSON ROUTING
86 lines (69 loc) • 2.69 kB
JavaScript
var fs = require('fs')
, path = require('path')
, _ = require('underscore')
, load = require('./load');
/**
* main function
*
* @param app
* @param userOptions
*/
module.exports = function (app, userOptions) {
/**
* default options
*
* @type {{routesPath: string, controllersPath: string, setup: string, vars: null}}
*/
var options = {
routesPath : './routes'
, controllersPath: './controllers'
, policyPath : './policy'
, processdir : process.cwd()
, cors : false
, displayRoute : true
, defaultAction : 'index'
, customGlobalPolicy : []
, excludeRoutes : []
};
// make sure userOptions is something
if (_.isUndefined(userOptions)) userOptions = {};
// combine any specified options with the defaults
_.extend(options, userOptions);
// get route files
var files = fs.readdirSync(options.routesPath);
var jsonFiles = _.filter(files, function (file) {
return path.extname(file) == '.json'
});
if(options.cors) {
app.use(function (req, res, next) {
var method = req.method && req.method.toUpperCase && req.method.toUpperCase();
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials','true');
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
// res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.header('Access-Control-Expose-Headers', 'X-My-Custom-Header, X-Another-Custom-Header');
if ('OPTIONS' == method) {
res.sendStatus(204).end();
}
else {
next();
}
});
}
if(options.displayRoute) {
console.log('');
console.log(' \x1b[34m********************************* ROUTES *******************************\x1b[0m');
}
jsonFiles.forEach(function (file) {
options.routeName = file.split('.')[0];
options.basePathJson = path.join(options.processdir, options.routesPath);
options.basePathController = path.join(options.processdir, options.controllersPath);
options.basePathPolicy = path.join(options.processdir, options.policyPath);
load(app, options);
});
if(options.displayRoute) {
console.log(' \x1b[34m************************************************************************\x1b[0m');
console.log('');
}
};