Windows 与 POSIX
【Windows vs. POSIX】
node:path 模块的默认操作因 Node.js 应用运行的操作系统而异。具体来说,当在 Windows 操作系统上运行时,node:path 模块会假定使用的是 Windows 风格的路径。
【The default operation of the node:path module varies based on the operating
system on which a Node.js application is running. Specifically, when running on
a Windows operating system, the node:path module will assume that
Windows-style paths are being used.】
因此,在 POSIX 和 Windows 上使用 path.basename() 可能会得到不同的结果:
【So using path.basename() might yield different results on POSIX and Windows:】
在 POSIX 上:
【On POSIX:】
path.basename('C:\\temp\\myfile.html');
// Returns: 'C:\\temp\\myfile.html' 在 Windows 上:
【On Windows:】
path.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html' 为了在任何操作系统上处理 Windows 文件路径时获得一致的结果,请使用 path.win32:
【To achieve consistent results when working with Windows file paths on any
operating system, use path.win32:】
在 POSIX 和 Windows 上:
【On POSIX and Windows:】
path.win32.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html' 要在任何操作系统上处理 POSIX 文件路径时获得一致的结果,请使用 path.posix:
【To achieve consistent results when working with POSIX file paths on any
operating system, use path.posix:】
在 POSIX 和 Windows 上:
【On POSIX and Windows:】
path.posix.basename('/tmp/myfile.html');
// Returns: 'myfile.html' 在 Windows 上,Node.js 遵循每个驱动器的工作目录概念。
当使用没有反斜杠的驱动器路径时,可以观察到这种行为。例如,path.resolve('C:\') 可能返回与 path.resolve('C:') 不同的结果。有关更多信息,请参见 这个 MSDN 页面。
【On Windows Node.js follows the concept of per-drive working directory.
This behavior can be observed when using a drive path without a backslash. For
example, path.resolve('C:\\') can potentially return a different result than
path.resolve('C:'). For more information, see
this MSDN page.】