Node.js v0.10.18 手册 & 文档
目录
路径 (Path)#
稳定度: 3 - 稳定
This module contains utilities for handling and transforming file paths. Almost all these methods perform only string transformations. The file system is not consulted to check whether paths are valid.
本模块包含一套用于处理和转换文件路径的工具集。几乎所有的方法仅对字符串进行转换, 文件系统是不会检查路径是否真实有效的。
Use require('path')
to use this module. The following methods are provided:
通过 require('path')
来加载此模块。以下是本模块所提供的方法:
path.normalize(p)#
Normalize a string path, taking care of '..'
and '.'
parts.
规范化字符串路径,注意 '..'
和 `'.' 部分
When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. On Windows backslashes are used.
当发现有多个连续的斜杠时,会替换成一个; 当路径末尾包含斜杠时,会保留; 在 Windows 系统会使用反斜杠。
Example:
示例:
path.normalize('/foo/bar//baz/asdf/quux/..')
// returns
'/foo/bar/baz/asdf'
path.join([path1], [path2], [...])#
Join all arguments together and normalize the resulting path.
组合参数中的所有路径,返回规范化后的路径。
Arguments must be strings. In v0.8, non-string arguments were silently ignored. In v0.10 and up, an exception is thrown.
参数必须是字符串。在 v0.8 版本非字符串参数会被悄悄忽略。 在 v0.10 及以后版本将会抛出一个异常。
Example:
示例:
path.join('foo', {}, 'bar')
// 抛出异常
TypeError: Arguments to path.join must be strings
path.resolve([from ...], to)#
Resolves to
to an absolute path.
将 to
参数解析为一个绝对路径。
If to
isn't already absolute from
arguments are prepended in right to left
order, until an absolute path is found. If after using all from
paths still
no absolute path is found, the current working directory is used as well. The
resulting path is normalized, and trailing slashes are removed unless the path
gets resolved to the root directory. Non-string arguments are ignored.
如果to
不是一个相对于from
参数的绝对路径,to
会被添加到from
的右边,直到找出一个绝对路径为止。如果使用from
路径且仍没有找到绝对路径时,使用当时路径作为目录。返回的结果已经规范化,得到的路径会去掉结尾的斜杠,除非得到的当前路径为root目录。非字符串参数将被忽略。
Another way to think of it is as a sequence of cd
commands in a shell.
另一种思考方式就是像在shell里面用一系列的‘cd’命令一样.
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
Is similar to:
相当于:
cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd
The difference is that the different paths don't need to exist and may also be files.
不同的是,不同的路径不需要存在的,也可能是文件。
Examples:
示例:
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// 如果当前工作目录为 /home/myself/node,它返回:
'/home/myself/node/wwwroot/static_files/gif/image.gif'
path.isAbsolute(path)#
Determines whether path
is an absolute path. An absolute path will always
resolve to the same location, regardless of the working directory.
判定path
是否为绝对路径。一个绝对路径总是指向一个相同的位置,无论当前工作目录是在哪里。
Posix examples:
Posix 示例:
path.isAbsolute('/foo/bar') // true
path.isAbsolute('/baz/..') // true
path.isAbsolute('qux/') // false
path.isAbsolute('.') // false
Windows examples:
Windows 示例:
path.isAbsolute('//server') // true
path.isAbsolute('C:/foo/..') // true
path.isAbsolute('bar\\baz') // false
path.isAbsolute('.') // false
path.relative(from, to)#
Solve the relative path from from
to to
.
解决从from
到to
的相对路径。
At times we have two absolute paths, and we need to derive the relative
path from one to the other. This is actually the reverse transform of
path.resolve
, which means we see that:
有时我们有2个绝对路径, 我们需要从中找出相对目录的起源目录。这完全是path.resolve
的相反实现,我们可以看看是什么意思:
path.resolve(from, path.relative(from, to)) == path.resolve(to)
Examples:
示例:
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
// 返回
'../../impl/bbb'
path.dirname(p)#
Return the directory name of a path. Similar to the Unix dirname
command.
返回路径中文件夹的名称. 类似于Unix的dirname
命令.
Example:
示例:
path.dirname('/foo/bar/baz/asdf/quux')
// returns
'/foo/bar/baz/asdf'
path.basename(p, [ext])#
Return the last portion of a path. Similar to the Unix basename
command.
返回路径中的最后哦一部分. 类似于Unix 的 basename
命令.
Example:
示例:
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// returns
'quux'
path.extname(p)#
Return the extension of the path, from the last '.' to end of string in the last portion of the path. If there is no '.' in the last portion of the path or the first character of it is '.', then it returns an empty string. Examples:
返回路径中文件的扩展名, 在从最后一部分中的最后一个'.'到字符串的末尾。 如果在路径的最后一部分没有'.',或者第一个字符是'.',就返回一个 空字符串。 例子:
path.extname('index')
// returns
''
path.sep#
The platform-specific file separator. '\\'
or '/'
.
特定平台的文件分隔工具. '\\'
或者 '/'
.
An example on *nix:
*nix 上的例子:
'foo/bar/baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
An example on Windows:
Windows 上的例子:
'foo\\bar\\baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
path.delimiter#
The platform-specific path delimiter, ;
or ':'
.
特定平台的路径分隔符, ;
或者 ':'
.
An example on *nix:
*nix 上的例子:
process.env.PATH.split(path.delimiter)
// returns
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
An example on Windows:
Windows 上的例子:
console.log(process.env.PATH)
// 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\'
process.env.PATH.split(path.delimiter)
// returns
['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\']
process.env.PATH.split(path.delimiter)
// returns
['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\']