如何在node.js的代码中使用命令提示符?
我正在编写一些代码,其中列出了由cmd运行的一些功能,如何在代码中包含cmd可执行文件?有一个全局函数,即:如何在node.js的代码中使用命令提示符?,node.js,cmd,command-prompt,Node.js,Cmd,Command Prompt,我正在编写一些代码,其中列出了由cmd运行的一些功能,如何在代码中包含cmd可执行文件?有一个全局函数,即: process.argv 或者,如果您想更好地接受命令行参数,请尝试npm package yargs下面是一个使用子进程的小示例 在cmd中执行命令 const { exec } = require("child_process"); function os_func() { this.execCommand = function(cmd, callbac
process.argv
或者,如果您想更好地接受命令行参数,请尝试
npm package yargs
下面是一个使用子进程的小示例
在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)
});
});
我一直在使用process.env,
并为cmd设置根目录,但它似乎不起作用。