我正在尝试执行exe file
。通过命令行执行它正常工作并打印我想要的数据。但是使用 node.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");
答案 0 :(得分:2)
只需按以下方式运行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");
聆听data
上的req
事件,将其切换为流媒体/流动模式
答案 1 :(得分:2)
试试这样。
我认为这会对你有所帮助。
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")