你需要在 node.js 中使用 path.join 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9756567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do you need to use path.join in node.js?
提问by balupton
as everyone knows Windows does paths with backslashes where Unix does paths with forward slashes. node.js provides path.join()to always use the correct slash. So for example instead of writing the Unix only 'a/b/c'you would do path.join('a','b','c')instead.
众所周知,Windows 使用反斜杠处理路径,而 Unix 使用正斜杠处理路径。node.js 规定path.join()始终使用正确的斜杠。因此,例如,不是只编写 Unix 'a/b/c',path.join('a','b','c')而是您会这样做。
However, it seems that despite this difference if you do not normalize your paths (e.g. using path.join) and just write paths like a/b/cnode.js has no problem with running your scripts on Windows.
但是,尽管存在这种差异,但如果您不标准化您的路径(例如使用 path.join)并且只编写像a/b/cnode.js这样的路径,那么在 Windows 上运行您的脚本是没有问题的。
So is there any benefit over writing path.join('a','b','c')over 'a/b/c'? Both appear to work regardless of platform...
那么,有没有写上任何好处path.join('a','b','c')了'a/b/c'?无论平台如何,两者似乎都可以工作......
采纳答案by ebohlman
Windows filesystems have no problem using either forward or backward slashes as path separators (this has been the case since back in the DOS days). The only real issue is that Windows command-line processors (or, more specifically, Windows-native command-line utilities) tend to interpret forward slashes as option specifiers rather than path components. Therefore, you need a backslashed path if you need to pass a path to a Windows command run as a subprocess. Also, Windows API calls (and methods from higher-level languages that call the Windows API) that return paths will use backslashes, so even if you aren't passing them to subprocesses, you'll need to normalize them.
Windows 文件系统使用正斜杠或反斜杠作为路径分隔符都没有问题(早在 DOS 时代就是这种情况)。唯一真正的问题是 Windows 命令行处理器(或更具体地说,Windows 本地命令行实用程序)倾向于将正斜杠解释为选项说明符而不是路径组件。因此,如果您需要将路径传递给作为子进程运行的 Windows 命令,则需要一个反斜杠路径。此外,返回路径的 Windows API 调用(以及来自调用 Windows API 的高级语言的方法)将使用反斜杠,因此即使您没有将它们传递给子进程,您也需要对其进行规范化。
回答by dronus
path.joinwill take care of unneccessary delimiters, that may occur if the given pathes come from unknown sources (eg. user input, 3rd party APIs etc.).
path.join将处理不必要的分隔符,如果给定的路径来自未知来源(例如用户输入、第 3 方 API 等),则可能会发生这种情况。
So path.join('a/','b')path.join('a/','/b'), path.join('a','b')and path.join('a','/b')will all give a/b.
因此path.join('a/','b')path.join('a/','/b'),path.join('a','b')与path.join('a','/b')将全部给予a/b。
Without using it, you usually would make expectations about the start and end of the pathes joined, knowing they only have no or one slash.
如果不使用它,您通常会对加入的路径的开始和结束做出预期,因为知道它们只有一个斜线或没有斜线。
回答by Timothy Strimple
I use path.jointo ensure folder separators are in the correct places, not necessarily to ensure that it uses forward versus back slashes. For example:
我path.join用来确保文件夹分隔符位于正确的位置,不一定要确保它使用正斜杠和反斜杠。例如:
path.join("/var/www", "test")
Will correctly insert the separator between www and test /var/www/test
将正确插入 www 和 test 之间的分隔符 /var/www/test
回答by Rafael Xavier
Short answer:
简短的回答:
All fs.*functions (eg. fs.open, etc) treats the pathname for you. So, you don't need to use path.joinyourself and make your code illegible.
所有fs.*函数(例如fs.open,等)都会为您处理路径名。因此,您不需要path.join自己使用并使您的代码难以辨认。
Long answer:
长答案:
All fs.*functions call path._makeLong(path), which in turn call path.resolve(path), which has special RegExps for Windows, which takes into account backslash \or forward slashes /. You can check it out for yourself looking their source code at:
所有fs.*函数都调用path._makeLong(path),而后者又调用path.resolve(path),它具有 Windows 的特殊 RegExp,它考虑了反斜杠\或正斜杠/。您可以在以下位置查看他们的源代码:

