-
-
Save swape/bd4b9375b223a35e36fc to your computer and use it in GitHub Desktop.
Require js files from a directory with glob based on a file name.
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
// ./controllers/b.js | |
module.exports = { | |
index: function(){ | |
console.log('b is here'); | |
} | |
}; |
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
// main file | |
var glob = require('glob'); | |
var path = require('path'); | |
// controller directory | |
var ctlPath = './controllers/*.js'; | |
// including based on names | |
glob.sync(ctlPath).forEach(function (file) { | |
var name = file.replace(/.\/controllers\/|.js/ig , ''); | |
GLOBAL[name] = require(path.resolve(file)); | |
}); | |
// calling index function inside file b.js | |
b.index(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment