注意,前情提示:
本代码基于《Node.js(nodejs)对本地JSON文件进行增、删、改、查操作(轻车熟路)》
在/api/demo/文件夹下面创建copyFileOrFolder.js和deleteFileOrFolder.js
copyFileOrFolder.js代码内容
const $g = global.SG.$g, fs = global.SG.fs, router = global.SG.router, path = global.SG.path;
module.exports = global.SG.router;
const copyFileOrFolerPath = "./json/demo/demo.json";//被复制的文件路径
const pasteFileOrFolerPath = "./json/demoBak/demoBak.json";//粘贴文件路径
// const copyFileOrFolerPath = "./json";//被复制的文件夹路径
// const pasteFileOrFolerPath = "./temp";//粘贴文件夹路径
const copyDir = (src, dst) => {
//读取目录中的所有文件/目录
fs.readdir(src, (err, paths) => {
if (err) throw err;
paths.forEach(path => {
const _src = src + "/" + path, _dst = dst + "/" + path;
let readable, writable;
fs.stat(_src, (err, st) => {
if (err) throw err;
if (st.isFile()) {//是文件
readable = fs.createReadStream(_src);//创建读取流
writable = fs.createWriteStream(_dst);//创建写入流
readable.pipe(writable);//通过管道来传输流
} else {//是文件夹
copyFolder(_src, _dst);//如果是目录则递归调用自身
}
});
});
});
};
//复制文件夹
const copyFolder = (src, dst) => fs.existsSync(dst) ? copyDir(src, dst) : fs.mkdir(dst, () => copyDir(src, dst));
const mkdirs = $g.dir.mkdirsByFilePath;//递归创建文件夹目录(基于文件路径)
const resJson = (req, res) => {
fs.copyFile(copyFileOrFolerPath, pasteFileOrFolerPath, err => {
$g.json.res(req, res, `复制${copyFileOrFolerPath}${err ? "失败" : "成功"}`, {err, copyFileOrFolerPath, pasteFileOrFolerPath}, true);
});
};
//复制并且粘贴(覆盖无提示)文件(all方法支持POST、GET、PUT、PATCH、DELETE传参方式)
router.all("/demo/copyFileOrFolder", (req, res) => {
if (fs.statSync(copyFileOrFolerPath).isFile()) {//是文件
mkdirs(pasteFileOrFolerPath, () => resJson(req, res));//复制文件
} else {//是文件夹
copyFolder(copyFileOrFolerPath, pasteFileOrFolerPath);//复制文件夹
$g.json.res(req, res, `复制${copyFileOrFolerPath}成功`, {err: null, copyFileOrFolerPath, pasteFileOrFolerPath}, true);
}
});
deleteFileOrFolder.js代码内容
const $g = global.SG.$g, fs = global.SG.fs, router = global.SG.router, path = global.SG.path;
module.exports = global.SG.router;
// const deleteFileOrFolderPath = "./json/demoBak/demoBak.json";//被删除文件路径
const deleteFileOrFolderPath = "./json/demoBak";//被删除文件夹路径
const deleteFileOrFolderRecursive = (url, callback) => {
//判断给定的路径是否存在
fs.exists(url, exists => {
if (exists) {
if (fs.statSync(url).isFile()) {//是文件
fs.unlink(url, err => callback(err));
} else {//是文件夹
let __del = (url, callback) => {
let files = [];
//判断给定的路径是否存在
files = fs.readdirSync(url);//返回文件和子目录的数组
files.forEach((file, index) => {
let curPath = path.join(url, file);
if (fs.statSync(curPath).isDirectory()) {
__del(curPath);//fs.statSync同步读取文件夹文件,如果是文件夹,在重复触发函数
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(url);//清除文件夹
callback && callback();
};
__del(url, callback);
}
} else {
callback("亲,您提供的是一个子虚乌有的路径!");
}
});
};
const resJson = (req, res, err) => $g.json.res(req, res, `删除${deleteFileOrFolderPath}${err ? "失败" : "成功"}`, {err, deleteFileOrFolderPath}, !err);
//删除文件or文件夹(覆盖无提示)文件(all方法支持POST、GET、PUT、PATCH、DELETE传参方式)
router.all("/demo/deleteFileOrFolder", (req, res) => deleteFileOrFolderRecursive(deleteFileOrFolderPath, err => resJson(req, res, err)));
在index.js最后一行加入
app.use(API_PATH, require(`.${API_PATH}/demo/copyFileOrFolder`));//复制文件or文件夹
app.use(API_PATH, require(`.${API_PATH}/demo/deleteFileOrFolder`));//删除文件or文件夹
运行
node index标签:fs,const,服务器端,err,res,json,文件夹,目录 From: https://blog.51cto.com/u_15920212/5962882