How to retrieve object from child_process stdout?
How to retrieve object from child_process stdout?
I want to run some process
on different instance than Node
server. I'm trying with child_process, I create some loop and execute child_process
.
process
Node
child_process
From child_process, I retrieve as Buffer
and In object format like follow.
Buffer
data: , status: 'active', success: true
How should I get these data as an object format?
const child = spawn('node', ['app.js']);
child.stdout.on('data', (data) =>
// How can I apply filter over to ignore some data
console.log(data) // print buffer
console.log(data.toString()) // print some stringify Obj
);
Is there any way we can send data for child_process
from app.js
except console.log
?
child_process
app.js
console.log
1 Answer
1
If app.js is a node script it's easy to send data to the caller (and vice-versa) for example:
index.js
const child_process = require('child_process');
const appInstance = child_process.fork('app.js', , );
appInstance.on('message', message =>
console.log('Message from app.js:', message, JSON.stringify(message));
);
app.js
// Send some data to caller.
const data = status: 'ok', message: 'hello from app.js' ;
if (process.send)
process.send(data);
If you want to filter there are loads of ways of doing this. In the message handler you can switch on the message object, e.g. if (message && message.status).
100 child_process
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is server requirement for running
100 child_process
single time?– higunjan
Sep 10 '18 at 13:34