使用node.js如何遍历目录?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

陇县网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
同步遍历
const fs = require('fs');
const path=require('path');
function travel(dir,callback){
fs.readdirSync(dir).forEach((file)=>{
var pathname=path.join(dir,file)
if(fs.statSync(pathname).isDirectory()){
travel(pathname,callback)
}else{
callback(pathname)
}
})
}
travel('F:/HTML/Node/test',function(pathname){
console.log(pathname)
})异步遍历
const fs = require('fs');
const path=require('path');
function travel(dir,callback){
fs.readdir(dir,(err,files)=>{
if(err){
console.log(err)
}else{
files.forEach((file)=>{
var pathname=path.join(dir,file)
fs.stat(pathname,(err,stats)=>{
if(err){
console.log(err)
}else if(stats.isDirectory()){
travel(pathname,callback)
}else{
callback(pathname)
}
})
})
}
})
}
travel('F:/HTML/Node/test',function(pathname){
console.log(pathname)
})关于使用node.js如何遍历目录问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。