我如何要求在node.js文件夹中的所有文件?
需要像这样的东西:
files.forEach(function (v,k){
// require routes
require('./routes/'+v);
}};
我如何要求在node.js文件夹中的所有文件?
需要像这样的东西:
files.forEach(function (v,k){
// require routes
require('./routes/'+v);
}};
当前回答
在这个glob解决方案上展开。如果你想将所有模块从一个目录导入到index.js中,然后将该index.js导入到应用程序的另一部分,那么就这样做。注意,stackoverflow使用的高亮显示引擎不支持模板文字,因此这里的代码可能看起来很奇怪。
const glob = require("glob");
let allOfThem = {};
glob.sync(`${__dirname}/*.js`).forEach((file) => {
/* see note about this in example below */
allOfThem = { ...allOfThem, ...require(file) };
});
module.exports = allOfThem;
完整的示例
目录结构
globExample/example.js
globExample/foobars/index.js
globExample/foobars/unexpected.js
globExample/foobars/barit.js
globExample/foobars/fooit.js
globExample - js操作。
const { foo, bar, keepit } = require('./foobars/index');
const longStyle = require('./foobars/index');
console.log(foo()); // foo ran
console.log(bar()); // bar ran
console.log(keepit()); // keepit ran unexpected
console.log(longStyle.foo()); // foo ran
console.log(longStyle.bar()); // bar ran
console.log(longStyle.keepit()); // keepit ran unexpected
globExample foobars / index . js
const glob = require("glob");
/*
Note the following style also works with multiple exports per file (barit.js example)
but will overwrite if you have 2 exports with the same
name (unexpected.js and barit.js have a keepit function) in the files being imported. As a result, this method is best used when
your exporting one module per file and use the filename to easily identify what is in it.
Also Note: This ignores itself (index.js) by default to prevent infinite loop.
*/
let allOfThem = {};
glob.sync(`${__dirname}/*.js`).forEach((file) => {
allOfThem = { ...allOfThem, ...require(file) };
});
module.exports = allOfThem;
globExample foobars /无法js。
exports.keepit = () => 'keepit ran unexpected';
globExample foobars / barit js。
exports.bar = () => 'bar run';
exports.keepit = () => 'keepit ran';
globExample foobars / fooit js。
exports.foo = () => 'foo ran';
在安装了glob的项目中,运行node example.js
$ node example.js
foo ran
bar run
keepit ran unexpected
foo ran
bar run
keepit ran unexpected
其他回答
当require得到一个文件夹的路径时,它会在该文件夹中查找index.js文件;如果有一个,它就使用它,如果没有,它就失败。
这可能是最有意义的(如果你可以控制文件夹)创建一个index.js文件,然后分配所有的“模块”,然后简单地要求。
yourfile.js
var routes = require("./routes");
index.js
exports.something = require("./routes/something.js");
exports.others = require("./routes/others.js");
如果你不知道文件名,你应该写一些加载器。
加载器的工作示例:
var normalizedPath = require("path").join(__dirname, "routes");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./routes/" + file);
});
// Continue application logic here
使用这个函数,你可以要求一个完整的目录。
const GetAllModules = ( dirname ) => {
if ( dirname ) {
let dirItems = require( "fs" ).readdirSync( dirname );
return dirItems.reduce( ( acc, value, index ) => {
if ( PATH.extname( value ) == ".js" && value.toLowerCase() != "index.js" ) {
let moduleName = value.replace( /.js/g, '' );
acc[ moduleName ] = require( `${dirname}/${moduleName}` );
}
return acc;
}, {} );
}
}
// calling this function.
let dirModules = GetAllModules(__dirname);
我建议使用glob来完成这个任务。
var glob = require( 'glob' )
, path = require( 'path' );
glob.sync( './routes/**/*.js' ).forEach( function( file ) {
require( path.resolve( file ) );
});
另一种选择是使用require-dir包,它允许您执行以下操作。它也支持递归。
var requireDir = require('require-dir');
var dir = requireDir('./path/to/dir');
我使用节点模块复制到模块来创建一个文件,以需要我们基于nodejs的系统中的所有文件。
我们的实用程序文件的代码是这样的:
/**
* Module dependencies.
*/
var copy = require('copy-to');
copy(require('./module1'))
.and(require('./module2'))
.and(require('./module3'))
.to(module.exports);
在所有的文件中,大多数函数都被写成export,如下所示:
exports.function1 = function () { // function contents };
exports.function2 = function () { // function contents };
exports.function3 = function () { // function contents };
所以,然后使用文件中的任何函数,你只需要调用:
var utility = require('./utility');
var response = utility.function2(); // or whatever the name of the function is