N8TRW: access a filesystem: reading, writing, renaming, and deleting files.
Since you’ll be touching the target file a lot to trigger changes automatically, you might want to use the watch command to do this automatically:
$ watch -n 1 touch target.txt
Run launch the watcher program using node, like so:
$ node watcher.js
$ node watcher-argv.js target.txt
Enhancing our file-watching example program even further by having it spawn a child process in response to a change.
$ node watcher-spawn.js target.txt
Sometimes you’ll want to capture data from a stream, rather than just piping it forward. Let’s see how to do that in capturing data from an EventEmitter
$ node watcher-spawn-parse.js target.txt
Node.js’s methods for reading and writing files. Along the way we’ll see two common error-handling patterns in Node.js: error events on EventEmitters and err callback arguments. There are a few approaches to reading and writing files in Node. The simplest is to read in or write out the entire file at once. This technique works well for small files. Other approaches read and write by creating Streams or staging content in a Buffer.
$ node write-simple.js
$ node read-simple.js
The cat.js uses a file stream to pipe a file’s data to standard output:
$ chmod +x cat.js
$ ./cat.js target.txt
You can also listen for data events from the file stream instead of calling pipe(). The following program called read-stream.js does this:
read-stream.js no-such-file
$ node
Example of how to read a file using the readFileSync() method:
const fs = require('fs');
const data = fs.readFileSync('target.txt');
process.stdout.write(data.toString());