使用 node.js 观察文件更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13698043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Observe file changes with node.js
提问by Sam
I have the following UseCase:
我有以下用例:
A creates a Chat and invites B and C - On the Server A creates a File. A, B and C writes messages into this file. A, B and C read this file.
A 创建聊天并邀请 B 和 C - 在服务器 A 上创建一个文件。A、B 和 C 将消息写入此文件。A、B 和 C 读取此文件。
I want a to create a file on server and observe this file if anybody else writes something into this file send the new content back with websockets.
我想在服务器上创建一个文件并观察这个文件,如果其他人在这个文件中写了一些东西,用 websockets 发送新的内容。
So, any change of this file should be observed by my node.js application.
因此,我的 node.js 应用程序应该观察到此文件的任何更改。
How can I observe files-changes? Is this possible with node js without locking the files?
如何观察文件更改?node js 是否可以在不锁定文件的情况下实现?
If not possible with files, would it be possible with database object (NoSQL)
如果文件无法使用,是否可以使用数据库对象(NoSQL)
回答by Henrik Andersson
Good news is that you can observe filechanges with Node's API.
好消息是您可以使用 Node 的 API 观察文件更改。
Reading the Node.js API doc is very useful when figuring these things out. It's an invaluable source. :)
在弄清楚这些事情时,阅读 Node.js API 文档非常有用。这是一个宝贵的资源。:)
This however doesn't give you access to the contents that has been written into the file.
You can maybe use the fs.appendFile();function so that when something is being written into the file you emit an event to something else that "logs" your new data that is being written.
但是,这不会让您访问已写入文件的内容。您可以使用该fs.appendFile();函数,以便在将某些内容写入文件时,向其他内容“记录”正在写入的新数据的其他内容发出事件。
fs.watch():Directly pasted from the docs
fs.watch():直接从文档粘贴
fs.watch('somedir', function (event, filename) {
console.log('event is: ' + event);
if (filename) {
console.log('filename provided: ' + filename);
} else {
console.log('filename not provided');
}
});
Read here about the fs.watch(); function
EDIT: You can use the function
编辑:您可以使用该功能
fs.watchFile();
Read here about the fs.watchFile(); function
在此处阅读有关 fs.watchFile(); 的信息。功能
This will allow you to watch a file for changes. Ie. whenever it is accessed by some other processes of any kind.
这将允许您查看文件的更改。IE。每当它被任何类型的其他进程访问时。
回答by Jonathan L?wenstern
Also you could use node-watch. Here's an easy example:
你也可以使用node-watch。这是一个简单的例子:
const watch = require('node-watch')
watch('README.md', function(event, filename) {
console.log(filename, ' changed.')
})
回答by Alfred
I do not think you need to have observe file changes or use a NoSQL database for this (if you do not want to). My advice would be to look at events(Observer pattern). There are more than enough tutorials on this topic available online (Google). For example Felix's article about Using EventEmitters
我认为您不需要为此观察文件更改或使用 NoSQL 数据库(如果您不想这样做)。我的建议是查看事件(观察者模式)。在线(谷歌)有足够多的关于这个主题的教程。例如 Felix 的关于Using EventEmitters的文章
This publish/subcribe semantic can also be achieved with NoSQL. In Redisfor example, I think you should have a look at pubsub.
这种发布/订阅语义也可以通过 NoSQL 实现。以Redis为例,我认为你应该看看pubsub。
In MongoDB I think tailable cursorsis what you are looking for. On their blog they have a post explaining pub/sub.

