在使用nodeJS的时候,记录一下。
在使用异步的时候不能直接返回路径,需要使用回调函数接收参数的形式返回文件路径:
//白名单
var root = path.join(path.resolve(process.argv[2] || ‘.’),‘static’);
var default_list = [‘default.html’,‘index.html’]
console.log(‘选择的目录:’,root);
//递归回调函数:查找白名单内的文件,找到返回
function checkFileEx(t_list,filepath,callback){
if(!t_list){
console.log(‘列表是空的’)
return callback(false)
}
var f_name = t_list.pop()
var full_path = path.join(filepath, f_name);
fs.stat(full_path,function(err,stat1){
if(!err){
callback(full_path);
}else{
return checkFileEx(t_list,filepath,callback);
}
});
}
使用递归查找白名单内的文件,并异步返回文件路径
最新推荐文章于 2024-09-12 15:08:36 发布