保留的根目录
🌐 The reserved root directory
只要挂载了任何虚拟文件系统,通过 node:fs 就可以读取保存挂载点的目录。vfs.vfsBase() 返回它的路径,path.join(os.devNull, 'vfs')。它包含每个已挂载文件系统的目录,目录名就像它的 vfs.mountPoint 的最后一段一样。
🌐 While any virtual file system is mounted, the directory that holds the mount
points can be read through node:fs. vfs.vfsBase() returns its path,
path.join(os.devNull, 'vfs'). It contains a directory for every mounted file
system, named like the last segment of its vfs.mountPoint.
const vfs = require('node:vfs');
const fs = require('node:fs');
const path = require('node:path');
const root = vfs.vfsBase();
const assets = vfs.create();
assets.writeFileSync('/logo.svg', '<svg/>');
const mountPoint = assets.mount();
const name = path.basename(mountPoint);
fs.readdirSync(root); // [ name ]
fs.readdirSync(root, { recursive: true }); // [ name, `${name}/logo.svg` ] 根目录本身是只读的。创建、删除或更改其条目会失败并显示 EROFS,而其条目所指向的文件系统可以像平常一样写入。当没有挂载任何东西时,根目录不存在。
🌐 The root directory itself is read-only. Creating, removing, or changing its
entries fails with EROFS, while the file systems its entries lead to can be
written to as usual. When nothing is mounted, the root directory does not exist.