node.js监视文件的创建时间 [英] node.js monitoring for when a file is created
问题描述
http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener
警告#
fs.watch API在各个平台上不是100%一致的,在某些情况下
不可用。可用性#
这个功能取决于底层操作系统提供
的方式来通知文件系统的变化。
在Linux系统上,它使用inotify。
在BSD系统(包括OS X)上,这使用kqueue。
在SunOS系统(包括Solaris和SmartOS)上,它使用事件端口。
在Windows系统上,此功能取决于ReadDirectoryChangesW。
如果底层功能由于某种原因不可用,那么
fs.watch将不会能够运作。例如,在网络文件系统(NFS,SMB等)上观看文件或
目录通常不会
可靠地工作或根本不工作。
您仍然可以使用fs.watchFile,它使用stat轮询,但它是慢
并且不太可靠。
I am writing a node.js program that needs to do something as soon as a file with a certain name is created in a specific directory. I could do this by doing repeated calls to fs.readdir until I see the name of the file, but I'd rather do something more efficient. Does anyone know of a way to do this?
Look at the fs.watch
(and fs.watchFile as a fallback) implementation, but realize it is not perfect:
http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener
Caveats#
The fs.watch API is not 100% consistent across platforms, and is unavailable in some situations. Availability#
This feature depends on the underlying operating system providing a way to be notified of filesystem changes.
On Linux systems, this uses inotify. On BSD systems (including OS X), this uses kqueue. On SunOS systems (including Solaris and SmartOS), this uses event ports. On Windows systems, this feature depends on ReadDirectoryChangesW.
If the underlying functionality is not available for some reason, then fs.watch will not be able to function. For example, watching files or directories on network file systems (NFS, SMB, etc.) often doesn't work reliably or at all.
You can still use fs.watchFile, which uses stat polling, but it is slower and less reliable.
这篇关于node.js监视文件的创建时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!