Created
May 17, 2017 22:34
-
-
Save Ikhan/ab4a62aa0e57e7b75ecb1b09cdded7da to your computer and use it in GitHub Desktop.
helper function for routes files in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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