📜  如何在 Node.js 中监视文件的修改?

📅  最后修改于: 2022-05-13 01:56:30.925000             🧑  作者: Mango

如何在 Node.js 中监视文件的修改?

在这篇文章中,我们将探讨如何在 Node.js 中监控文件的修改。有时,我们可能需要查看对特定文件所做的任何更改,并在该文件的基础上执行某种类型的函数。要查看对文件系统所做的任何更改,我们可以使用 node.js 提供的 fs 模块,它提供了一个解决方案。为了监视任何修改的文件,我们可以使用 fs.watch() 或 fs.watchFile()函数。

两者的区别在于 fs.watch() 方法可以同时监视文件和目录,而 fs.watchFile 仅用于监视文件中的任何更改。

fs.watch()是 fs 模块提供的内置应用程序编程接口方法。这会定期监视给定目录中文件的任何更改。此方法返回一个 Watcher 对象,该对象基本上用于跟踪文件中的任何更改。

句法:

fs.watch()

fs.watch( filename[, options][, listener] )

fs.watchFile()方法持续监视给定文件中的任何更改。它包含一个回调侦听器,该侦听器始终侦听文件并通知任何更改。

fs.watchFile()

fs.watchFile(filename[, options], listener)

参数:上述方法接受 3 个参数,解释如下:

  1. filename:这是一个字符串、缓冲区或 URL,表示要监视的文件的名称(fs.watch() 中的目录)。
  2. options:这是用于修改方法行为的字符串或对象。这是一个可选参数。它还具有以下参数:
    1. 持久性:这是一个布尔值,指定在监视文件时进程是否应该继续。默认值是true。
    2. recursive:这是一个布尔值,指定是否应监视给定目录的子目录。默认值为假。
    3. encoding:这是一个字符串值,它指定需要用于将文件名传递给用户的字符编码。
  3. 侦听器:这是一个字符串值,它指定使用传递给侦听器的文件名的字符编码。
    1. eventType:这是一个字符串值,指定对文件的不同类型的修改。
    2. 文件名:顾名思义,这需要文件名的字符串或缓冲区输入。

以下示例说明了在对 Node.js 中的文件进行任何更改或修改时触发的触发事件:

示例 1:此示例显示了借助fs.watch()方法对文件进行的任何更改:

Javascript
// Node.js program to demonstrate the
// fs.watch() method
 
// Importing the filesystem module
const fs = require('fs');
 
fs.watch("example.txt", (eventType, filename) => {
  console.log("The file ", filename, " was modified!");
 
  // We can look for different types of changes on a file
  // using the event type like: rename, change, etc.
  console.log("It was a ", eventType, " event type.");
});
 
// Changing the contents of the file
setTimeout(
() => fs.writeFileSync("example.txt",
"Welcome to Geeks for Geeks, The file is modified"), 1000);


Javascript
// Node.js program to demonstrate
// the fs.watchFile() method
 
// Importing the filesystem module
const fs = require('fs');
 
fs.watchFile("example.txt", {
 
  // Passing the options parameter
  bigint: false,
  persistent: true,
  interval: 1000,
}, (curr, prev) => {
  console.log("\nThe file was edited");
 
  // Time when file was updated
  console.log("File was modified at: ", prev.mtime);
  console.log("File was again modified at: ", curr.mtime);
  console.log(
    "File Content Updated: ",
    fs.readFileSync("example.txt", "utf8")
  );
});
 
// Make Changes to the file for the first time
fs.writeFileSync("example.txt",
"Welcome to Geeks for Geeks");
 
// Make Changes to the file for the second time
setTimeout(
() => fs.writeFileSync("example.txt",
    "File is Edited Again!!!"),
3000
);


输出:

The file example.txt was modified!

It was a change event type.

注意:上述方法不可靠,每次修改都会显示多个事件。这种方法不可靠,每次修改都可能显示多个事件。

示例 2:此示例显示了借助fs.watchFile()方法对文件进行的任何更改:

Javascript

// Node.js program to demonstrate
// the fs.watchFile() method
 
// Importing the filesystem module
const fs = require('fs');
 
fs.watchFile("example.txt", {
 
  // Passing the options parameter
  bigint: false,
  persistent: true,
  interval: 1000,
}, (curr, prev) => {
  console.log("\nThe file was edited");
 
  // Time when file was updated
  console.log("File was modified at: ", prev.mtime);
  console.log("File was again modified at: ", curr.mtime);
  console.log(
    "File Content Updated: ",
    fs.readFileSync("example.txt", "utf8")
  );
});
 
// Make Changes to the file for the first time
fs.writeFileSync("example.txt",
"Welcome to Geeks for Geeks");
 
// Make Changes to the file for the second time
setTimeout(
() => fs.writeFileSync("example.txt",
    "File is Edited Again!!!"),
3000
);

输出: