监听文件
node 监听一个文件的变化
// 监听一个文件的创建删除
function watchFileStatus (filePath) {
const watcherpath = path.resolve(filePath);
const listenerFunc = (curr,prev)=>{
const ctime = Date.parse(curr.mtime);
const ptime = Date.parse(prev.mtime);
if (ctime > 0 && (ptime == 0 || ctime == ptime)) {
// 文件被创建
} else if (ctime == 0 && ptime == 0) {
// 文件不存在
} else if (ctime == 0 && ptime > 0) {
// 文件被删除
} else {
// 文件被编辑
}
}
fs.watchFile(watcherpath, { interval: 2000 }, listenerFunc);
}
本页目录