This article mainly describes how to use the path. dirname method in node. js. This article describes the method description, syntax, usage examples, and source code implementation of path. dirname. For more information, see
Method description:
Returns the path directory. Similar to UNIX directory commands.
Syntax:
The Code is as follows:
Path. dirname (p)
Because this method belongs to the path module, You need to introduce the path module (var path = require ("path") before use "))
Receiving parameters:
P path address
Example:
The Code is as follows:
Var path = require ("path ");
Path. dirname ('/foo/bar/baz/asdf/quux ')
// Returns
'/Foo/bar/baz/asdf'
Source code:
The Code is as follows:
Exports. dirname = function (path ){
Var result = splitPath (path ),
Root = result [0],
Dir = result [1];
If (! Root &&! Dir ){
// No dirname whatsoever
Return '.';
}
If (dir ){
// It has a dirname, strip trailing slash
Dir = dir. substr (0, dir. length-1 );
}
Return root + dir;
};