javascript - How to read files in directory in node.js? -
in node.js app, load modules folder, , put them in array. right manually this
var siteslist = [ require('./js/sites/a.js'), require('./js/sites/b.js'), require('./js/sites/c.js') ]; but on time, don't want add file name list everytime add new module. how can loop through js files in sites folder , auto add them in array?
var normalizedpath = require("path").join(__dirname, "js/sites"); var siteslist = []; var fs = require("fs"); fs.readdirsync(normalizedpath).foreach(function(file) { siteslist.push(require("./js/sites/" + file)); }); you can module:
Comments
Post a Comment