May 10, 2021 Node.js
稳定性: 3 - 稳定
The node .js path module contains a set of tools for processing and converting file paths. B
asically all repetitions are converted only to strings.
The file system does not check that the path is valid.
You can
require('path')
const path = require('path');
The Node .js module contains the methods described below:
For normalizing paths, note
'..'
and
'.'
When more than one slash is found, it is replaced with a slash. When the end of the path contains a slash, it is preserved.
The Windows system uses a backslash.
For example:
path.normalize('/foo/bar//baz/asdf/quux/..')
// returns
'/foo/bar/baz/asdf'
Used to connect all parameters and normalize the output path.
The argument must be a string. I n v0.8, non-character parameters are ignored. An exception was thrown after the version after v0.10.
For example:
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns
'/foo/bar/baz/asdf'
path.join('foo', {}, 'bar')
// throws exception
TypeError: Arguments to path.join must be strings
The ability
to
the to parameter to an absolute path.
If the
to
is not an absolute path relative
from
argument
to
from
of the from until an absolute path is found. I
f you use all
from
parameters and still don't find an absolute path, the current working directory will be used. T
he path returned has been normalized and the tail slash has been removed (unless it is the root).
Arguments that are not strings are ignored.
Another idea is to execute a series of cd commands in
cd
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
Similar to:
cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd
The difference is that different paths don't need to exist, or they can be files.
For example:
path.resolve('/foo/bar', './baz')
// returns
'/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/')
// returns
'/tmp/file'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if currently in /home/myself/node, it returns
'/home/myself/node/wwwroot/static_files/gif/image.gif'
Determines whether the parameter
path
is an absolute path.
An absolute path resolves to the same location, regardless of where the current working directory is.
Posix example:
path.isAbsolute('/foo/bar') // true
path.isAbsolute('/baz/..') // true
path.isAbsolute('qux/') // false
path.isAbsolute('.') // false
Examples of Windows:
path.isAbsolute('//server') // true
path.isAbsolute('C:/foo/..') // true
path.isAbsolute('bar\\baz') // false
path.isAbsolute('.') // false
Resolves
from
path from
to
to.
Sometimes we have 2 absolute paths from which we need to find a relative directory.
This
path.resolve
path.resolve(from, path.relative(from, to)) == path.resolve(to)
For example:
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
// returns
'..\\..\\impl\\bbb'
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
// returns
'../../impl/bbb'
Returns the directory
p
is located.
Similar to the
dirname
command.
For example:
path.dirname('/foo/bar/baz/asdf/quux')
// returns
'/foo/bar/baz/asdf'
Returns the last part of the path.
Similar to the
basename
command.
For example:
path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// returns
'quux'
Returns the extension of path
p
from the last '.' t
o the end of the string. I
f the last part doesn't have '.', or if the path is '.' a
t the beginning, an empty string is returned.
For example:
path.extname('index.html')
// returns
'.html'
path.extname('index.coffee.md')
// returns
'.md'
path.extname('index.')
// returns
'.'
path.extname('index')
// returns
''
A file separator for a particular platform,
'\\'
or
'/'
Examples on nix:
'foo/bar/baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
Examples of Windows:
'foo\\bar\\baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
The separator for a particular platform,
;
Or
':'
Examples on nix:
console.log(process.env.PATH)
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter)
// returns
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
Examples of 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\\']
The object that returns the path string.
Examples on nix:
path.parse('/home/user/dir/file.txt')
// returns
{
root : "/",
dir : "/home/user/dir",
base : "file.txt",
ext : ".txt",
name : "file"
}
Examples of Windows:
path.parse('C:\\path\\dir\\index.html')
// returns
{
root : "C:\\",
dir : "C:\\path\\dir",
base : "index.html",
ext : ".html",
name : "index"
}
Return the path string from the object, as
path.parse
path.format({
root : "/",
dir : "/home/user/dir",
base : "file.txt",
ext : ".txt",
name : "file"
})
// returns
'/home/user/dir/file.txt'
The
path
path access above is provided, but always interacts in a posix-compatible manner.
The path path
path
above is provided, but always interacts in a win32-compatible manner.