我正在寻找从 node.js 应用程序调用 Windows 命令行的解决方案或 NPM。
我想要的是调用一些批处理文件并在带有 node.js 的机器上运行它们,当然还有参数并读取它们的输出。
您可以为此使用标准模块child_process.spawn()。
从文档示例中:
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
替换'ls'
为'c:/windows/system32/cmd.exe'
和['-lh', '/usr']
以['/c', 'batfile.bat']
运行批处理文件batfile.bat
。