Use of the path. join method in node. js _ node. js

Source: Internet
Author: User
This article describes how to use the path. join method in node. js. This article describes the method description, syntax, examples of use, and source code implementation of path. join. For more information, see Method description:

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

Syntax:

The Code is as follows:


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

Because this method belongs to the path module, You need to introduce the path module (var path = require ("path") before use "))

Example:

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:

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
// At least one more non-slash character.
//
// Note that for normalize () to treat a path as an UNC path it needs
// 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 );
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.