官术网_书友最值得收藏!

File events

Most applications make some use of the filesystem, in particular, those that function as web services. As well, a professional application will likely log information about usage, cache pre-rendered data views, or make other changes to files and directory structures. Node allows developers to register for notifications on file events through the fs.watch method. The watch method broadcasts changed events on both files and directories.

The watch method accepts three arguments, in order:

  • The file or directory path being watched. If the file does not exist, an ENOENT (no entity) error will be thrown, so using fs.exists at some prior useful point is encouraged.
  • An optional options object, including:
    • Persistent (Boolean default true): Node keeps processes alive, as long as there is something to do. Set this option to false to let Node close the process even if your code still has a file watcher watching.
    • Recursive (Boolean default false): Whether to automatically descend into subdirectories. Note: This is not consistently implemented across platforms. For this reason, and for performance reasons, you should explicitly control the file list you are watching, rather than randomly watching directories.
    • Encoding (String default utf8): Character encoding of passed filenames. You probably don't need to change this.
  • The listener function, which receives two arguments:
    • The name of the change event (one of rename or change)
    • The filename that was changed (important when watching directories)

This example will set up a watcher on itself, change its own filename, and exit:

const fs = require('fs');
fs.watch(__filename, { persistent: false }, (event, filename) => {
console.log(event);
console.log(filename);
})

setImmediate(function() {
fs.rename(__filename, __filename + '.new', () => {});
});

Two lines, rename and the name of the original file, should have been printed to the console.

Close your watcher channel whenever you want to use code like this:

let w = fs.watch('file', () => {});
w.close();

It should be noted that fs.watch depends a great deal on how the host OS handles file events, and the Node documentation says this:

"The fs.watch API is not 100% consistent across platforms, and is unavailable in some situations."

The author has had very good experiences with the module across many different systems, noting only that the filename argument is null in callbacks on OS X implementations. Different systems may also enforce case sensitivity, one way or the other. Nevertheless, be sure to run tests on your specific architecture — trust, but verify.

Alternatively, use a third-party package! If you encounter difficulties with a Node module, check npm for alternatives. Here, as a problem-fixing wrapper on top of fs.watch, consider Paul Miller's chokidar. It is used as the file-watching tool for build systems like gulp, and in many other projects. Refer to: https://www.npmjs.com/package/chokidar.

主站蜘蛛池模板: 麦盖提县| 连城县| 枝江市| 永济市| 闻喜县| 肃北| 垣曲县| 武胜县| 高要市| 宜丰县| 衡阳县| 无为县| 建昌县| 稻城县| 甘南县| 宜阳县| 阳新县| 青海省| 贵溪市| 正宁县| 淮安市| 大石桥市| 洛川县| 霍山县| 方正县| 奇台县| 石景山区| 土默特右旗| 阿鲁科尔沁旗| 岳普湖县| 建瓯市| 金平| 宁国市| 巴林左旗| 霸州市| 罗山县| 崇礼县| 鄂州市| 马龙县| 黄冈市| 娄烦县|