// 实现一个文件系统读写数据库 const fs = require('fs'); const path = require('path'); var fileName='./DB.json'; const FS = { // 读数据 read: function (key) { fs.readFile(fileName, (err, data) => { const json = data ? JSON.parse(data) : {} console.log(json[key]) }) }, write: function (key, value = '') { if(!fs.existsSync(fileName)){ //创建文件 fs.writeFileSync(fileName,0); } fs.readFile(fileName, (err, data) => { const json = data ? JSON.parse(data) : {} json[key] = value fs.writeFile(fileName, JSON.stringify(json), err => { console.log('写入成功') } ) }) }, }; module.exports = FS;