在 Node.js 中生成一个子进程,数据未定义
spawn a child process in node.js, data is undefined
这里我试图执行一个exe file
. 通过命令行执行时,它工作正常并打印我想要的数据。但是使用节点.js它将数据打印为undefined
.这是我使用的代码
var server = http.createServer(function (req, res) {
switch (req.url) {
case '/start':
console.log("begin...............");
req.on("data", function (value) {
exec('CALL hello.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
});
req.on("end", function () {
console.log(data); // prints undefined
console.log(JSON.stringify(data));
console.log("hello");
console.log("before--------------");
res.writeHead(200);
res.end(data);
});
break;
}
});
server.listen(8080);
console.log("Server running on the port 8080");
试试这样。
我认为这会对你有所帮助。
var http = require('http');
var exec = require('child_process').execFile;
var server = http.createServer(function (req, res) {
switch (req.url) {
case '/start':
console.log("begin...............");
exec('hello.exe', function(err, data) {
console.log(err);
console.log(data);
console.log(data); // prints undefined
console.log(JSON.stringify(data));
console.log("hello");
console.log("before--------------");
res.writeHead(200);
res.end(data);
});
break;
}
});
server.listen(8080);
console.log("Server running on the port 8080")
只需像下面这样运行exec
调用。
var http = require('http');
var exec = require('child_process').exec;
var server = http.createServer(function (req, res) {
switch (req.url) {
case '/start':
console.log("begin...............");
exec('CALL hello.exe', function(err, data) {
console.log(err);
console.log(data);
console.log(data); // prints undefined
console.log(JSON.stringify(data));
console.log("hello");
console.log("before--------------");
res.writeHead(200);
res.end(data);
});
break;
}
});
server.listen(8080);
console.log("Server running on the port 8080");
在req
上侦听 data
事件会将其切换到流式传输/流模式
http://nodejs.org/api/stream.html#stream_event_data
相关文章:
- 子进程未接收消息
- 如何通过子进程连接Java应用程序后在node.js中编写文件
- 节点.js子进程中的运行函数
- 如何将画布对象从子进程流式传输回 Node.js
- 如何在Firefox插件中向子进程发送消息,如Chrome原生消息
- 从生成的子进程中获取信息的最佳方法
- 在 Node.js 中使用 Jasmine 测试子进程发送
- 在快速服务器中生成子进程
- 如何在 node.js 中关闭子进程的 stdio 管道
- 如何使用并行子进程在大型数组上执行“工作”
- 使用 node.js (子进程) 运行 shell 脚本
- 如何从nodejs中的守护进程生成gpg子进程
- Cloud9 在生成子进程时抛出错误,是否有解决方法
- 如何在从 Meteor.method 返回之前等待子进程结果
- 将输入数据发送到节点.js中的子进程
- 完成时的子进程事件
- 如何在 Debian 上通过 node.js 中的引用变量停止子进程
- 如何动态生成将对象转移到另一个Node进程的代码
- Node.js为什么不立即启动子进程
- 如何在node.js中启动一个全新的进程(不是子进程)