Recently I was playing with the exercises from the fantastic http://nodeschool.io. Below is my solution to “Make it modular” exercise. Of course you can improve it eg. with the use of Regex but I just wanted to keep it simple for the start.
So the first file with the name myprogram.js:
var module = require("./module.js"); module(process.argv[2],process.argv[3], function(err , list){ if(err) return console.log("Error:", err) for(var s =0; s<list.length; s++){="" console.log(list[s])="" }="" });<="" pre="">
and the second module.js:
module.exports = function(path, extension, callback){ var fs = require('fs'); var flist = []; fs.readdir(path, function(err, list){ if(err){ return callback(err); } for (var i =0; i< list.length; i++){ if(list[i].indexOf("." + extension) >-1){ flist.push(list[i]); } } return callback(null,flist) }) }