Node.js监视文件的变化并解析它们 [英] Node.js monitor file for changes and parse them

查看:127
本文介绍了Node.js监视文件的变化并解析它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要监控文件的变化。由于此文件有大量新条目,我需要监控此文件。我需要将新插入的内容添加到此文件中以便能够解析此内容。

I need to monitor a file for changes. Due to a large amount of new entries to this file I would need to 'monitor' this file. I would need to get the new inserted content to this file to be able to parse this content.

我发现此代码:

fs.watchFile('var/log/query.log', function() {
console.log('File Changed ...');
//how to get the new line which is now inserted?
});


推荐答案

在bash上你会用<$做类似的事情c $ c> tail --follow 。

还有一个包 tail 可用。
你可以在一个文件上观看,并获得一个事件的新行:

There is also a package tail availible. you can watch on a file, and get new lines with an event:

const Tail = require('tail').Tail;
var tail = new Tail("var/log/query.log");
tail.watch()
tail.on("line", data => {
  console.log(data);
});

这篇关于Node.js监视文件的变化并解析它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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