The path module, as its name implies, is used by Node to provide us with paths to files and directories. Today we will look at some variables and functions we use in path.
I. Path module
After the previous study, we know that the path module is not in the global, so when we need to use it, we need to require, and it is a built-in module, we do not need to write path.
const Path = require('path');Copy the code
Similarly, some operations of the path module will be different depending on the operating system. For example, POSIX and Windows are described in the documentation. Windows is well known, but POSIX is something that I think some people may not know. Portable Operation System Interface, which is POSIX, is a standard developed to improve compatibility and portability of applications. Because in the early days it was hard to have different standards. We can learn more about the details in the afternoon.
Second, basic methods
2.1 Path.basename (), path.extName (), path.dirname()
These three methods return the last part of the path, with extname returning the extension as the name implies and dirname returning the directory name.
2.2 the path. The normalize ()
This method is used to deal with paths that we might have written wrong, like we wrote too many slashes, or we wrote relative paths, absolute paths, and so on, and it will help us to make this path the correct path.
2.3 the path. The join ()
This method concatenates the path fragments we pass in, while also calling path.normalize() to return the correct path.
2.4 the path. The resolve ()
This function helps us to convert relative paths to absolute paths, which is very useful.
2.5 the path. The parse (), the path. The format ()
These two methods are obvious opposites, formatting and parsing.
Note that format has precedence over arguments. Root is ignored if dir exists, ext and name are ignored if base exists.
One interesting thing about these paths is that the./ relative path, in require, refers to the current folder, whereas elsewhere it refers to the directory path executed by Node.
PS: a holiday home, blocked. Today I have learned some methods about path, and I feel that there are many places to use it. I hope to consolidate it more, thank you! Actually recently all the time have the idea that thinks oneself do a blog, wait for free oneself knock 1.