节点WritableStream不直接级联

时间:2018-11-02 16:01:04

标签: javascript node.js stream filesystems

我正在使用写入流my.log写入日志文件fs.createWriteStream('my.log', {flags: 'a'})

同时,我有另一个代码段正在使用fs.createReadStream()来读取my.log文件,但是,似乎没有在写入日志文件后立即获得更新。仅当我在操作系统(Windows)上手动打开和关闭文件时,它才会被触发。

如何使此触发立即发生?我是否每次都重新打开和关闭writestream?

var strm = fs.createWriteStream('my.log', {flags: 'a'})
// log my data
strm.close();
// rinse and repeat

1 个答案:

答案 0 :(得分:0)

看来这归结为Node / Windows中的问题:

  • “ fs.watch API在各个平台上并非100%一致,并且在某些情况下不可用”
  • “在Windows系统上,此功能取决于ReadDirectoryChangesW。”

https://nodejs.org/api/fs.html#fs_availability

作为解决方案,我现在使用ive:

// https://www.npmjs.com/package/tail
var Tail = require('tail').Tail;
// Pass property to force the usage of fs.watchFile
tail = new Tail('my.log', { useWatchFile: true });

如果您不想使用“ tail”软件包,则应手动使用fs.watchFile并根据文件更改采取相应的措施。

相关问题