新手求助 想执行linux命令并拿到结果,这样写哪里不对?
var http = require("http");
var sys = require('util');
var spawn = require('child_process').spawn;
function onRequest(request, response) { //这是createServer的回调函数(事件驱动)
console.log("Request received.");
var mon = spawn("ls", ["-l"]);
var mondata;
mon.stdout.on('data', function (data) {
sys.puts(data);
console.log(data)
mondata = data
})
response.writeHead(200, {
"Content-Type" : "text/plain"
});
response.write('aaa' + mondata);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
1 回复
已经解决了。。把response写到stdout.on的回调里即可。。还是对异步不理解啊。。