最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - watch file for new content and retrieve that content in NodeJS - Stack Overflow

programmeradmin1浏览0评论

I am monitoring the file for content change using Node.js watch File. Its succesfully calling the event when content of the file is modified

fs.watchFile(filePath, ()=> {
    console.log('File Changed ...');
    file = fs.readFileSync(filePath);
    console.log('File content at : ' + new Date() + ' is \n' + file);
});

from Hello to Hello World

I want only the World not all the contents of the file, can any one suggest the most efficient way to achieve this or any node package to achieve this. I looked at node package Chokidar but that also monitors the change.

I found a JAVA Solution for that, but not sure of any Node.js alternative for that.

How to watch file for new content and retrieve that content

I did my research tried to follow these posts

.html#fs_fs_watchfile_filename_options_listener

.html

Observe file changes with node.js

Thanks

I am monitoring the file for content change using Node.js watch File. Its succesfully calling the event when content of the file is modified

fs.watchFile(filePath, ()=> {
    console.log('File Changed ...');
    file = fs.readFileSync(filePath);
    console.log('File content at : ' + new Date() + ' is \n' + file);
});

from Hello to Hello World

I want only the World not all the contents of the file, can any one suggest the most efficient way to achieve this or any node package to achieve this. I looked at node package Chokidar but that also monitors the change.

I found a JAVA Solution for that, but not sure of any Node.js alternative for that.

How to watch file for new content and retrieve that content

I did my research tried to follow these posts

https://nodejs/api/fs.html#fs_fs_watchfile_filename_options_listener

http://www.codingdefined./2015/09/how-to-monitor-file-for-modifications.html

Observe file changes with node.js

Thanks

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Jan 27, 2017 at 5:19 AkashAkash 1411 silver badge5 bronze badges 2
  • it's not possible at low level, you'll have to capture file content beforehand and do your own diffing on change - similar to what your Java example does – Andrey Sidorov Commented Jan 27, 2017 at 5:54
  • Thanks @AndreySidorov, will in that case I will dig more into this and to try to find efficient algorithms to achieve that. – Akash Commented Jan 27, 2017 at 14:54
Add a ment  | 

1 Answer 1

Reset to default 6

The best way to do it is by creating a stream that reads the file and remains open for file changes. I found this tool that describes and solves this problem:

tailing-stream is a Node.js module that provides a Stream that can read continuously from a file that's being actively written to. This is in contrast to the standard fs.createReadStream. method, which returns a ReadableStream that stops reading once it gets to the last byte that existed at the time the stream was originally opened. It supports exactly the same interface as a Node ReadableStream, and its createReadStream method functions the same as fs.createReadStream.

https://github./jasontbradshaw/tailing-stream

You can install it using npm:

npm install tailing-stream

And here's a simple example:

const fs = require('fs');
const TailingReadableStream = require('tailing-stream');

const stream = TailingReadableStream.createReadStream("file", {timeout: 0});

stream.on('data', buffer => {
  console.log(buffer.toString());
});
stream.on('close', () => {
  console.log("close");
});
发布评论

评论列表(0)

  1. 暂无评论