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).






What is server requirement for running 100 child_process single time?

– higunjan
Sep 10 '18 at 13:34



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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)