This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ProgBlog/app/base/util/route-loader.js

30 lines
598 B
JavaScript
Raw Normal View History

2016-02-19 12:58:16 -05:00
'use strict';
const glob = require('glob');
/**
* Map Routes to route files
*
* @param {string} path - folder with Routes
* @return {Object} - Object mapping routes to their files
*/
module.exports = function routeLoader(path) {
const basePath = path.replace(/\\/g, '/');
let paths = glob.sync(`${path}/**/*.js`);
paths = paths.sort();
paths = paths.map((path) => path.replace('\\', '/'));
let routes = {};
paths.forEach((path) => {
let routePath = path.replace(basePath, '')
.replace('.js', '')
.replace('index', '');
routes[routePath] = path;
});
return routes;
};