Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS child process stdout are all numbers

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.

like image 999
Lee Avatar asked Apr 28 '15 08:04

Lee


People also ask

How does Nodejs handle child process?

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.

What is child_process spawn?

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.

What is the difference between fork () and spawn () methods in node JS?

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.

Is process stdout a stream in node JS?

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.


1 Answers

data is an array buffer. Call its toString method JSON.stringify(data.toString('utf8'))

like image 127
Miguel Mota Avatar answered Oct 26 '22 13:10

Miguel Mota