I'm writing some node.js scripts to kick off a child process. Code snippet is as below.
var spawn = require('child_process').spawn;
var child = spawn ('node', ['script.js'])
child.stdout.on('data',
function (data) {
logger.verbose('tail output: ' + JSON.stringify(data));
}
);
child.stderr.on('data',
function (data) {
logger.error('err data: ' + data);
}
);
Script runs well except that child process's stdout and stderr prints only numeric outputs:
Sample output:
108,34,44,34,105,110,99,114,101,97,109,101,110,116,97,108,95,112,111,108,108,105
How do I convert these numeric values to readable string?
Thanks.
Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.
spawn() or child_process. spawnSync() . child_process. exec() : spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete.
Spawn is useful when you want to make a continuous data transfer in binary/encoding format — e.g. transferring a 1 Gigabyte video, image, or log file. Fork is useful when you want to send individual messages — e.g. JSON or XML data messages.
If you've been using Node. js for a while, you've definitely run into streams. HTTP connections are streams, open files are streams; stdin, stdout, and stderr are all streams as well.
data
is an array buffer. Call its toString method JSON.stringify(data.toString('utf8'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With