Observe file changes with node.js

后端 未结 3 728
栀梦
栀梦 2020-11-28 08:39

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,

相关标签:
3条回答
  • 2020-11-28 09:03

    Good news is that you can observe filechanges with Node's 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.watch(): Directly pasted from the docs

    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

    This will allow you to watch a file for changes. Ie. whenever it is accessed by some other processes of any kind.

    0 讨论(0)
  • 2020-11-28 09:10

    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

    This publish/subcribe semantic can also be achieved with NoSQL. In Redis for example, I think you should have a look at pubsub.

    In MongoDB I think tailable cursors is what you are looking for. On their blog they have a post explaining pub/sub.

    0 讨论(0)
  • 2020-11-28 09:19

    Also you could use node-watch. Here's an easy example:

    const watch = require('node-watch')
    
    watch('README.md', function(event, filename) {
      console.log(filename, ' changed.')
    })
    
    0 讨论(0)
提交回复
热议问题