【发布时间】:2019-05-25 02:01:29
【问题描述】:
我是 node.js 的新手,并试图理解语法以及代码中逐行发生的确切情况。此代码运行带有一个参数 arg1 的文件 hello.js,并需要一些时间来执行。文件控制台每隔几秒钟就会注销它正在处理的当前命令。我想知道:
1) 如何停止文件运行(出现问题时中止);
2) 如何将当前命令,即文件被控制台注销,传回客户端;
3) 什么',孩子;'部分语法和 '.on(' 部分语法。非常感谢。
此代码在服务器端运行,由客户端的 jQuery POST 发起。客户端向服务器端发送一个参数,服务器将该参数作为参数传递到文件中并运行它。当文件运行时,它使用 console.log 每隔几秒输出一次信息。
我想将该信息实时传递回 jQuery 帖子,以便可以在客户端实时获取每个命令。我也不理解代码中的 '.on('data', )' 部分。 .on 是否意味着“完成”或“继续执行此操作”,而且我不理解“.exec,孩子;”语法的一部分。
var myExecute = require('child_process').exec, child;
var shellScript = myExecute('node hello.js arg1'); //run a file with an argument
shellScript.stdout.on('data', (data)=>{
console.log(data);//file output console logs appearing here in real time
});
shellScript.stderr.on('data', (data)=>{
console.error(data);
});```
I am not getting any error messages. Currently the file is running and accepting the argument param passed in. I want to be able to abort this process from the client side once it had begun and I want to get back the data that the file is console logging and pass it back to the client side jQuery POST in real time.
【问题讨论】:
-
能否在问题中添加
myExecute的定义? node中有多种执行命令的方式,知道你在用哪一种会很有帮助
标签: node.js child-process