Home > Web Front-end > JS Tutorial > Instructions for using the path.join method in node.js_node.js

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

WBOY
Release: 2016-05-16 16:28:18
Original
1674 people have browsed it

Method description:

Combine multiple parameters into a path (please see the example for details)

Grammar:

Copy code The code is as follows:

path.join([path1], [path2], [...])

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

Example:

Copy code The code is as follows:

path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns
'/foo/bar/baz/asdf'
path.join('foo', {}, 'bar')
// throws exception
TypeError: Arguments to path.join must be strings

Source code:

Copy code The code is as follows:

// windows version
exports.join = function() {
Function f(p) {
If (!util.isString(p)) {
           throw new TypeError('Arguments to path.join must be strings');
        }
Return p;
}

var paths = Array.prototype.filter.call(arguments, f);
var joined = paths.join('\');

// Make sure that the joined path doesn't start with two slashes, because
// normalize() will mistake it for an UNC path then.
//
// This step is skipped when it is very clear that the user actually
// intended to point at an UNC path. This is assumed when the first
// non-empty string arguments starts with exactly two slashes followed by
// at least one more non-slash character.
//
// Note that for normalize() to treat a path as an UNC path it needs to
// have at least 2 components, so we don't filter for that here.
// This means that the user can use join to construct UNC paths from
// a server name and a share name; for example:
// path.join('//server', 'share') -> '\\server\share')
If (!/^[\/]{2}[^\/]/.test(paths[0])) {
Joined = joined.replace(/^[\/]{2,}/, '\');
}

Return exports.normalize(joined);
};
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