chensenlai 10988628a0 语音房项目初始化 | %!s(int64=5) %!d(string=hai) anos | |
---|---|---|
.. | ||
.npmignore | %!s(int64=5) %!d(string=hai) anos | |
License | %!s(int64=5) %!d(string=hai) anos | |
Readme.md | %!s(int64=5) %!d(string=hai) anos | |
index.js | %!s(int64=5) %!d(string=hai) anos | |
package.json | %!s(int64=5) %!d(string=hai) anos |
An easy way to require all files within a directory.
var controllers = require('require-all')({
dirname : __dirname + '/controllers',
filter : /(.+Controller)\.js$/,
excludeDirs : /^\.(git|svn)$/
});
// controllers now is an object with references to all modules matching the filter
// for example:
// { HomeController: function HomeController() {...}, ...}
If your objective is to simply require all .js and .json files in a directory you can just pass a string to require-all:
var libs = require('require-all')(__dirname + '/lib');
If your directory contains files that all export constructors, you can require them all and automatically construct the objects using resolve
:
var controllers = require('require-all')({
dirname : __dirname + '/controllers',
filter : /(.+Controller)\.js$/,
excludeDirs : /^\.(git|svn)$/,
resolve : function (Controller) {
return new Controller();
}
});