Trying to execute shell command(any) from the browser and printing the result on the Ui using child_process.
unable to fetch the results from command line asynchronously.Am I missing something here ?
const exec = require('child_process').exec;
app.post('/plan',(req, res) => {
let cmd = exec('dir');
let output = "";
cmd.stdout.on('data', (data) => {
//console.log(`stderr: ${data}`);
output += data;
});
res.send(output); //not working
console.log(output); //its empty
cmd.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
cmd.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
});