Latest web development tutorials

Node.js Path Module

Node.js Path Module

Node.js tool module Node.js tool module

Node.js path module provides a number of small tools for working with file paths, we can be introduced into the module in the following ways:

var path = require("path")

method

No. Method & description
1 path.normalize (p)
Standardization route, pay attention to '..' and '.'.
2 path.join ([path1] [, path2 ] [, ...])
For the connection path. The main purpose of this method is that the current system will correct path separator, Unix systems is "/", Windows systems are "\."
3 path.resolve ([from ...], to )
The parameterto resolve to an absolute path.
4 path.isAbsolute (path)
Determine whether thepath parameter is an absolute path.
5 path.relative (from, to)
For the relative path into an absolute path.
6 path.dirname (p)
On behalf of the return path of the file folder, similar to the same Unix dirname command.
7 path.basename (p [, ext])
The last part of the return path. With the Unix command bashname similar.
8 path.extname (p)
Returns the extension part of the file path, the path of the last '' later. If a path does not contain '' or the path contains only one '' and that this '' is the first character of the path, this command returns an empty string.
9 path.parse (pathString)
Returns object path string.
10 path.format (pathObject)
Returns path string from the object, and path.parse contrary.

Attributes

No. Property & Description
1 path.sep
Platform file path delimiter, '\\' or '/'.
2 path.delimiter
Platform separator,; or ':'.
3 path.posix
Provide the above path method, but always posix compatible way interaction.
4 path.win32
Provide the above path method, but always win32 compatible way interaction.

Examples

Create main.js file, the code is as follows:

var path = require("path");

// 格式化路径
console.log('normalization : ' + path.normalize('/test/test1//2slashes/1slash/tab/..'));

// 连接路径
console.log('joint path : ' + path.join('/test', 'test1', '2slashes/1slash', 'tab', '..'));

// 转换为绝对路径
console.log('resolve : ' + path.resolve('main.js'));

// 路径中文件的后缀名
console.log('ext name : ' + path.extname('main.js'));

Code execution results are as follows:

$ node main.js 
normalization : /test/test1/2slashes/1slash
joint path : /test/test1/2slashes/1slash
resolve : /web/com/1427176256_27423/main.js
ext name : .js

Node.js tool module Node.js tool module