Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:28:11
Original
1870 people have browsed it

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;
};
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template