Home  >  Article  >  Web Front-end  >  Instructions for using the path.dirname method in node.js_node.js

Instructions for using the path.dirname method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:28:111868browse

Method description:

Return the directory of path. Similar to UNIX directory commands.

Grammar:

Copy code The code is as follows:

path.dirname(p)

Since this method belongs to the path module, the path module needs to be introduced before use (var path= require(“path”) )

Receive parameters:

p path address

Example:

Copy code The code is as follows:

var path= require("path");
path.dirname('/foo/bar/baz/asdf/quux')
// returns
'/foo/bar/baz/asdf'

Source code:

Copy 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;
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn