简体   繁体   English

Node.js生成过程如何使用stdout seperatley的每一行在函数中使用

[英]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.

相关问题 我可以在node.js Express应用程序中使用集群吗? - Can I use cluster in a node.js express app still spawn child_process workers for specific requests node.js child_process spawn stdout lower highWaterMark - node.js child_process spawn stdout lower highWaterMark Node.js:捕获`child_process.spawn`的STDOUT - Node.js: Capture STDOUT of `child_process.spawn` node.js child_process.spawn no stdout除非&#39;inherit&#39; - node.js child_process.spawn no stdout unless 'inherit' 每行上的Node.js child.spawn标准输出“实时” - Node.js child.spawn stdout “live” at every line node.js - 如何调整 child_process.spawn() 的标准输出缓冲区大小(又名 highWaterMark)? - node.js - How to adjust the the stdout buffer size (aka highWaterMark) of child_process.spawn()? Node.js使用生成两个子进程,然后两个子进程如何相互交换? - Node.js use spawn two child processes, then how two child processes exchange with each other? Node.js如何使用它并调用函数 - Node.js how to use this and call functions 如何使用从 Node.js 到 JavaScript 的函数和库? - How can I use functions and libraries from Node.js to JavaScript? 何时需要在node.js中使用process.nextTick以获得最佳实践异步功能? - When do I need to use process.nextTick in node.js for best practice async functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM