我如何将chmod与Node.js一起使用

问题描述:

如何将chmod与Node.js一起使用?

How do I use chmod with Node.js?

fs中有一个方法应该执行此操作,但是我不知道第二个参数是什么.

There is a method in the package fs, which should do this, but I don't know what it takes as the second argument.

fs.chmod(路径,模式,[回调])

异步chmod(2).除了可能的异常外,没有其他参数被赋予完成回调.

Asynchronous chmod(2). No arguments other than a possible exception are given to the completion callback.

fs.chmodSync(路径,模式)

同步chmod(2).

Synchronous chmod(2).

(来自 Node.js文档)

如果我做类似的事情

fs.chmodSync('test', 0755);

什么也没有发生(文件未更改为该模式).

nothing happens (the file isn't changed to that mode).

fs.chmodSync('test', '+x');

也不起作用.

我正在Windows机器上工作.

I'm working on a Windows machine btw.

根据其源代码和第203行:

function modeNum(m, def) {
  switch (typeof m) {
    case 'number': return m;
    case 'string': return parseInt(m, 8);
    default:
      if (def) {
        return modeNum(def);
      } else {
        return undefined;
      }
  }
}

它使用八进制数字或字符串.

it takes either an octal number or a string.

例如

fs.chmodSync('test', 0755);
fs.chmodSync('test', '755');

这不适用于您的情况,因为文件模式仅存在于* nix机器上.

It doesn't work in your case because the file modes only exist on *nix machines.