I need to call to CMD command from my node JS application , is it possible ?
I try with the following (POC) and I got error
var express = require('express');
var app = express();
app.get('/', function (req, res) {
function cmd_exec(cmd, args, cb_stdout, cb_end) {
var spawn = require('child_process').spawn,
child = spawn(cmd, args),
me = this;
me.exit = 0; // Send a cb to set 1 when cmd exits
child.stdout.on('data', function (data) {
cb_stdout(me, data)
});
child.stdout.on('end', function () {
cb_end(me)
});
}
foo = new cmd_exec('npm', 'install glob --save',
function (me, data) {
me.stdout += data.toString();
},
function (me) {
me.exit = 1;
}
);
setTimeout(
// wait 0.25 seconds and print the output
log_console,
250);
function log_console() {
console.log(foo.stdout);
}
res.send("Hello world");
});
I saw this code in the following link
node.js shell command execution
The error is :
TypeError: Incorrect value of args option
in line child = spawn(cmd, args),
what am I doing wrong here ?Currnlty I just use the npm install command(just for testing) but any other command that I can execute and run will be sufficient