您是否需要在node.js中使用path.join? [英] Do you need to use path.join in node.js?

查看:88
本文介绍了您是否需要在node.js中使用path.join?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,Windows使用带反斜杠的路径,而Unix使用带反斜杠的路径. node.js提供了 path.join() ,以始终使用正确的斜杠.因此,例如,可以只使用path.join('a','b','c')而不是只编写Unix 'a/b/c'.

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.

但是,即使您不对路径进行规范化(例如,使用path.join)并且仅编写a/b/c node.js之类的路径,在Windows上运行脚本也没有问题.

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/c node.js has no problem with running your scripts on Windows.

那么写path.join('a','b','c')而不是'a/b/c'有什么好处吗?不论平台如何,两者似乎都能正常工作.

So is there any benefit over writing path.join('a','b','c') over 'a/b/c'? Both appear to work regardless of platform...

推荐答案

Windows文件系统使用正斜杠或反斜杠作为路径分隔符都没有问题(从DOS时代开始就是这种情况).唯一真正的问题是Windows命令行处理器(或更具体地说,Windows本地命令行实用程序)倾向于将正斜杠解释为选项说明符而不是路径组成部分.因此,如果需要将路径传递给作为子进程运行的Windows命令,则需要反斜杠路径.同样,返回路径的Windows API调用(以及来自高级语言的调用Windows API的方法)将使用反斜杠,因此,即使不将其传递给子流程,也需要对其进行规范化.

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.

这篇关于您是否需要在node.js中使用path.join?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆