我在nodejs服务器上运行了一个资源加载器JS文件。我想放入一堆资源js文件,资源加载器将在初始化时加载到一个数组中。
// resource loader js file
var Resources = (function () {
var resources = {};
function Resources(){
... loading code here
// example of use after loading
resources[key].doStuff();
}
})();
module.exports.Resources = Resources;
在同一目录中,开发人员可以根据需要放入新的resource.js文件。
// resource file
var ResourceA = (function () {
function ResourceA(){
... loading code here
// example of use after loading
ResourceA.prototype.doStuff= function () {
// do stuff
};
})();
答案 0 :(得分:5)
我不确定你的意思,但如果你想要的是在服务器端的一个文件夹中加载一堆模块,我就构建了这个小函数来做到这一点。
var path = require('path'),
walk = require('walk');
function findModules(opts,done){
var walker = walk.walk(opts.folder, { followLinks: false }),
modules = [];
walker.on('file', function(root, stat, next) {
var current = path.join(root, stat.name),
extname = path.extname(current);
if(extname === '.js' && (opts.filter === undefined || opts.filter(current))){
var module = require(current);
modules.push(module);
}
next();
});
walker.on('end', function() {
done(modules);
});
}
然后,你这样称呼它:
findModules({
folder: 'path/to/folder',
filter: undefined // either undefined or a filter function for module names
}, function(modules){
// continue
});
安装 walk :
npm install walk --save
答案 1 :(得分:2)
新的更简单:
安装强>
let b = list.reduce((final_list, new_item) => {
final_list.map((smaller_list) => {
if (smaller_list.indexOf((new_item.id)) >=0) {
smaller_list[1].concat(new_item.first)
// not sure what to do here...
} else {
// not sure what to do here either...
}
})
x = final_list.concat([[item.id, [item.first]]])
return x
}, [])
<强>用法强>
npm install eloader
<强> GIT中强> https://github.com/augustoeliezer/eloader