nodejs statSync判断一个目录或文件是否存在
获取目录下某个文件中的信息 [ { name, photoUrl, ...}, ]
目录: 项目名称/app/public/json_tb/user_1/blog_1/commiter_2.json
getCommitForCurBlog (blogId, authorId, committerId) {
let curPath = '/user_' + authorId + '/blog_' + blogId + '/committer_' + committerId + '.json'
let url = './app/public/json_tb' + curPath
let jsonData = []
// console.log('fs.statSync(url):', path.resolve(url))
// console.log('fs.statSync(url):', fs.statSync(path.resolve(url)) )
try {
let statObx = fs.statSync(path.resolve(url))
// console.log('isFile(): ', statObx.isFile(), statObx.isDirectory())
if (statObx.isFile()) {
jsonData = require('@app/public/json_tb' + curPath)
// console.log('>>>>>>>>>')
}
} catch (e) {
// nodejs.ENOENTError: ENOENT: no such file or directory
}
// console.log('>>>>>>>>>', jsonData)
return jsonData
}
网友评论