I am writing some code that lists some functionalities which are run by cmd, How can I enclose cmd executables in my code?
2 Answers
Here is a small example to use child_process.
Execute command in cmd.
const { exec } = require("child_process");
function os_func() {
this.execCommand = function(cmd, callback) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
callback(stdout);
});
}
}
app.get("/", (req, res) => {
console.log("inside get");
var os = new os_func();
os.execCommand('arp -a', function (returnvalue) {
res.end(returnvalue)
});
});
Comments
There is a global function i.e:
process.argv
Or if you want to take command-line arguments a lot better try npm package yargs.
1 Comment
Digital Nomad
I have been using
process.env, and I set the root for cmd, but it seems not working.