Skip to content

Instantly share code, notes, and snippets.

@Ikhan
Created May 17, 2017 22:34
Show Gist options
  • Save Ikhan/ab4a62aa0e57e7b75ecb1b09cdded7da to your computer and use it in GitHub Desktop.
Save Ikhan/ab4a62aa0e57e7b75ecb1b09cdded7da to your computer and use it in GitHub Desktop.
helper function for routes files in nodejs
//helper/controller.js
var pathModule = require('path');
var fs = require('fs');
exports.pathLocation = function () {
var basePath = pathModule.resolve(__dirname,'../controllers');
var controllers = {};
var paths = fs.readdirSync(basePath);
paths.forEach(function(path){
var files = fs.readdirSync(pathModule.join(basePath, path));
controllers[path] = {};
// // Filter out hidden files
files = files.filter(function(file){ return !( file.indexOf('.')===0 ); });
files.forEach(function(file) {
var key = file.replace(/\.\w{2,3}$/, '');
controllers[path][key] = require(pathModule.join(basePath, path, file));
});
});
return controllers;
}
// routes/users/index.js
var router = require('express').Router();
router.get('/get/:id', require('./get-user.js'));
router.post('/new', require('./new-user.js'));
router.post('/delete/:id', require('./delete-user.js'));
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment