【问题标题】:Run synchronous tasks using node.js on windows在 Windows 上使用 node.js 运行同步任务
【发布时间】:2013-07-08 21:19:18
【问题描述】:

我正在使用 Node FFI 模块并尝试在 Windows 上运行同步任务。我可以使用以下代码成功运行任务。

var ffi=require('ffi')
var nativeC = new ffi.Library("Kernel32", {
"WinExec": ["int32", ["string"]]
});

nativeC.WinExec('ls -lrt');

我认为这是执行同步任务的方式,但此代码总是在第一个“ls -lrt”命令之后退出;如果我链接更多命令,它们将无法工作。那么,这里是否有一个回调函数,在 FFI 模块中,或者我可以在 Windows 上的 node.js 中链接命令的另一种方式,以便它们一个接一个地同步运行。

【问题讨论】:

  • ls 在 Windows outside of PowerShell 中通常不可用(并且无法识别 -lrt 选项)。该命令在cmd.exe 中有效吗?对于多个用户?
  • 所以,我正在使用 Git bash,所以我可以使用 ls,我想发布它,所以很抱歉我没有。
  • 试试nativeC.WinExec('bash -c "ls -lrt"');。即使您使用bash 执行脚本,WinExec() doesn't seem to use it(注意它的第二个参数)。

标签: node.js windows synchronization node-ffi


【解决方案1】:

我不确定您是否需要 WinExec 来运行 Windows 命令。正如乔纳森所指出的, ls 不可用。

但是,如果你想链接命令,你可以像这样使用 async.js 和 exec:

var
  async = require('async'); 
  exec = require('child_process').exec,
  commands = [ 'dir /w', 'echo test'];

var executeCommand = function(command, callback){
  exec(command, function (err, stdout, stderr) {
    if(err) return callback(err);
    console.log(stdout);
    callback();
  });
};

async.eachSeries(commands, executeCommand, function(err){
  console.log('error: ' + err);
});

【讨论】:

  • 谢谢,这就是我要找的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多