Node.js读取某个目录下的所有文件夹名字并将其写入到json文件

针对解决的问题是,有些时候我们需要读取某个文件并将其写入到对应的json文件(xml文件也行,不过目前用json很多,json是主流)。

源码如下:
index.js

var fs = require('fs');

let components = []
const files = fs.readdirSync(./)
files.forEach(function (item,index) {
    let stat = fs.lstatSync("" + item)
    if (stat.isDirectory() === true) { 
      components.push(item)
    }
})

console.log(components);

let str = JSON.stringify(components)
 
 fs.writeFile(./extension.json,str,function(err){
 if (err) {res.status(500).send(Server is error...)}
})

控制台输出对应的数据:

参考资料如下:
nodejs写入json文件,格式化输出json的方法:http://www.cnblogs.com/threeEyes/p/10023827.html
利用nodejs对本地json文件进行增删改查:https://blog.csdn.net/zhaoxiang66/article/details/79894209

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...