蓝山帝景
node.js没有提供直接创建嵌套文件夹的方法,如果需要创建嵌套的文件夹 则要用到 回调函数或者递归来完成。如下为递归的实现。[javascript] view plain copyvar fs = require('fs');var path = require('path');//使用时第二个参数可以忽略function mkdir(dirpath,dirname){//判断是否是第一次调用if(typeof dirname === "undefined"){if(fs.existsSync(dirpath)){return;}else{mkdir(dirpath,path.dirname(dirpath));}}else{//判断第二个参数是否正常,避免调用时传入错误参数if(dirname !== path.dirname(dirpath)){mkdir(dirpath);return;}if(fs.existsSync(dirname)){fs.mkdirSync(dirpath)}else{mkdir(dirname,path.dirname(dirname));fs.mkdirSync(dirpath);}}}mkdir('/home/ec/a/b/c/d');