[英]Node.js spawn process how can i use each line of stdout seperatley to use in functions
okay so i have my nodejs script below 好吧,所以我下面有我的nodejs脚本
var app = require('express')();
var server = require('http').createServer(app);
var bin = "casperjs";
var args = ['Backend.js'];
var io = require('socket.io').listen(server);
server.listen(process.env.PORT);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client/index.html');
});
io.sockets.on('connection', function (socket) {
console.log("socket connection incoming");
});
var spawn = require('child_process').spawn,
child = spawn(bin, args);
child.stdin.setEncoding = 'utf-8';
child.stdout.on('data', function (data) {
when (data = "Well Hello1") {
console.log("Working1");
}
if (data = "Well Hello2") {
console.log("Working2");
}
if (data = "Well Hello.") {
console.log("Working");
}
});
The problems is my if statements are not handled correctly. 问题是我的if语句处理不正确。
I have figured out i need to handle the stdout line by line and not as a stream can any one show me a solution please 我已经知道我需要逐行处理stdout,而不是像流一样,任何人都可以给我看一个解决方案。
You might find that using the event-stream
module makes this much easier. 您可能会发现使用
event-stream
模块使此操作变得更加容易。
You can then do something like this: 然后,您可以执行以下操作:
var es = require('event-stream');
es.pipeline(
child.stdout,
es.split(),
es.map(function (line) {
if (line === 'Well Hello1') {
console.log("Working1");
}
})
);
You'll need to npm install event-stream
first. 您需要先
npm install event-stream
。
Here's a new version of your full script: 这是完整脚本的新版本:
var app = require('express')();
var server = require('http').createServer(app);
var bin = "casperjs";
var args = ['Backend.js'];
var io = require('socket.io').listen(server);
server.listen(process.env.PORT);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client/index.html');
});
io.sockets.on('connection', function (socket) {
console.log("socket connection incoming");
});
var spawn = require('child_process').spawn,
child = spawn(bin, args);
child.stdin.setEncoding = 'utf-8';
var es = require('event-stream');
es.pipeline(
child.stdout,
es.split(),
es.map(function (line) {
if (line === 'Well Hello1') {
console.log("Working1");
}
})
);
EDIT : A simple a script that demonstrates spawning /bin/cat
, setting up a pipeline to split and then log what is written line-by-line, and sometimes write back to child.stdin
. 编辑 :一个简单的脚本,演示了生成
/bin/cat
,设置管道以进行拆分,然后记录逐行写入的内容,有时还写回到child.stdin
。 If you run this script it will hang waiting for input to cat
, but you will see what is output and you can play with it easily: 如果运行此脚本,它将挂起,等待
cat
输入,但是您将看到输出内容,并且可以轻松地使用它:
var es = require('event-stream');
var spawn = require('child_process').spawn;
var child = spawn('/bin/cat');
var count = 0;
var stream = es.pipeline(
child.stdout,
es.split(),
es.map(function (line) {
console.log("es.map(" + line + "):");
if (count < 3) {
child.stdin.write("Hello(" + count + ")\n");
}
count++;
})
);
child.stdin.write("Testing1\nTesting2");
child.stdin.write("Testing3\nTesting4\n");
child.stdin.write("Testing5\nTesting6\n");
stream.on('data', function (line) {
console.log(line);
if (line === '') {
console.log('end');
stream.end();
}
});
Here's a simpler example, for ping: 这是ping的一个简单示例:
var split = require('split');
var spawn = require('child_process').spawn;
var child = spawn('ping', ['www.google.com']);
child.stdout.pipe(split()).on('data', function (line) {
console.log('**' + line + '**');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.