路径分隔符 across os 平台

path separator across os platforms

来自 nodejs documentation fs.mkdtemp

const tmpDir = '/tmp';
const subdir = '/com.domain.app';

!fs.existsSync(tmp + subdir) ? fs.mkdirSync(tmp + subdir) : null;

// This method is *CORRECT*:
const path = require('path');
fs.mkdtemp(tmpDir + path.sep + subdir + path.sep, function(err, folder){
  if (err) throw err;
  console.log(folder);
});

我的问题与 path.sep 和临时目录有关,我希望代码与平台无关,并且能够 运行 在多个平台上。

  1. path.sep 在所有 nodejs 平台上的价值是多少。
  2. tmp 目录在所有 nodejs 平台上的价值是多少。
  3. /tmp 在 windows 可用吗?
  4. 关于临时的上述代码的任何建议directories/paths

谢谢

对跨平台代码使用 os.tmpDirpath.join 函数。

var tmp = require('os').tmpDir();
var dest = path.join(tmp, "com.domain.app");

!fs.existsSync(dest) ? fs.mkdirSync(dest) : null;

参考。 Writing cross platform node