node.js监视文件的创建时间 [英] node.js monitoring for when a file is created

查看:158
本文介绍了node.js监视文件的创建时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个node.js程序,只要在特定目录中创建具有特定名称的文件,就需要做一些事情。我可以通过重复调用fs.readdir来做到这一点,直到我看到文件的名称,但我宁愿做一些更有效的事情。有没有人知道这样做?

c $ c>(和fs.watchFile作为后备)的实现,但意识到这并不完美:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆