如何解决使用Node.js“ {流式传输”文件的每一行
我想逐一读取添加到文件中的所有新行,就像它是数据流一样。到目前为止,我正在使用chokidar来接收关于该文件的每次更新的通知,然后,我正在使用read-last-lines传递数字1
来表示我只想读一个线。
import fs from 'fs';
const readLastLines = require('read-last-lines');
import chokidar from 'chokidar';
const logFile = 'log.txt';
// first checks if its exist
if (fs.existsSync(logFile)) {
// then start watching on every update
chokidar.watch(logFile).on('change',(_event,_path) => {
// read each update
readLastLines.read(logFile,1)
.then((line: string) => {
// do something with this received line.
// Can't receive two or three lines if sent instantaneously
doSomething(line);
}).catch((err :any) => {
console.log(err)
})
})
} else {
console.log('File Not Found')
}
我工作得很好,除了即时获得两三个文件更新。在这种情况下,我只能得到此更新序列的最后一行。我认为chokidar
可能会延迟操作,或者我的操作系统不允许该节点程序同时读取另一个程序正在写入的文件。
有人猜我该如何解决?如果它是终端上的程序输出(stdout),我希望它准确运行。 (准确地说,我的海豚是使用它来从客户端记录在我的服务器上运行的程序的。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。