Reputation: 2372
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
.
From child_process, I retrieve as Buffer
and In object format like follow.
{ 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
?
Upvotes: 1
Views: 408
Reputation: 30675
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).
Upvotes: 1