The spawn()
function of the child_process
library is very similar to the exec()
function, except that instead of returning buffers, it returns a stream. Streams are extremely handy for certain circumstances. For example, what if we wanted to take the output of one command and send that as the input for a second command? The spawn()
function can help with this.
In our example, we will use spawn
to execute a simple CoffeeScript statement and retrieve the results.
We will be using the native Node library for this example.
In this example, we will demonstrate the use of spawn()
to execute a CoffeeScript statement:
Begin by loading the
child_process
library and grabbing itsspawn()
function:spawn = require('child_process').spawn
Define our CoffeeScript statement:
coffeeCode = 'console.log "The answer to life is #{6 * 7}"'
In our example, we want two processes:
echo
andcoffee
. Theecho
process will place our CoffeeScript statement into...