November 2013
Intermediate to advanced
148 pages
3h 12m
English
Let’s enhance our file-watching example program even further by having it spawn a child process in response to a change. To do this, we’ll bring in Node’s child-process module and dive into some Node patterns and classes. You’ll also learn how to use streams to pipe data around.
To keep things simple, we’ll make our script invoke the ls command with the -lh options. This will give us some information about the target file whenever it changes. You can use the same technique to spawn other kinds of processes, as well.
Open your editor and enter this:
| file-system/watcher-spawn.js | |
| | "use strict"; |
| | const |
| | fs = require('fs'), |
| | spawn = require('child_process').spawn, |
| | filename = process.argv[2]; |
| | |
| | if (!filename) ... |
Read now
Unlock full access