January 2018
Intermediate to advanced
336 pages
7h 22m
English
EventEmitter is a very important class in Node.js. It provides a channel for events to be dispatched and listeners to be notified. Many objects you’ll encounter in Node.js inherit from EventEmitter, like the Streams we saw in the last section.
Now let’s modify our previous program to capture the child process’s output by listening for events on the stream. Open an editor to the watcher-spawn.js file from the previous section, then find the call to fs.watch(). Replace it with this:
| | fs.watch(filename, () => { |
| | const ls = spawn('ls', ['-l', '-h', filename]); |
| | let output = ''; |
| | |
| | ls.stdout.on('data', chunk => output += chunk); |
| | |
| | ls.on('close', () => { |
| | ... |
Read now
Unlock full access