I am trying to execute an command via child_process
module and reponse the result to client browser as below, console.log(stdout)
work find, but the response.write()
doesen't work.
function start(response) {
dir_list(function(stdout) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
console.log(stdout);
response.end();
});
function dir_list(callback) {
var exec = require('child_process').exec
child = exec('ls -la', function(error, stdout, stderr) {
callback(stdout);
});
}
Above code is refractored by me which I refer to Node.js: Object value as stdout of child_process.exec, but, unluckly it doesn't work for me.
BTW, my originally code is:
function start(response) {
console.log("Request handler 'start' was called.");
var exec = require('child_process').exec;
exec("ls -lah", function (error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
console.log(stdout);
response.end();
});
Your code is missing a close }
at the end of the start
function.
function start(response) {
dir_list(function(stdout) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
console.log(stdout);
response.end();
});
}
function dir_list(callback) {
var exec = require('child_process').exec
child = exec('ls -la', function(error, stdout, stderr) {
callback(stdout);
});
}