Path module of Nodejs

Source: Internet
Author: User

Previous words

The path module contains a set of tools to process and convert file paths, which can be accessed by using the require(‘path‘) Module. This article describes the path module in detail

Path composition

"path.dirname (p)"

Returns the directory where the path p is located

var path = require (' path '); console.log (path.dirname ('/foo/bar/baz/asdf/a.txt '));  // /foo/bar/baz/asdfconsole.log (path.dirname ('/foo/bar/baz/asdf/'));  // /foo/bar/bazconsole.log (path.dirname (' c:/test/aaa '));  // c:/test

"path.basename (p[, ext])"

Returns the last part of the path, which is the file Name. The parameter ext is the suffix content that needs to be cut off

var path = require (' path '); console.log (path.basename ('/foo/bar/baz/asdf/a.txt '));  // a.txtconsole.log (path.basename ('/foo/bar/baz/asdf/a.txt ', '. txt '));  // aconsole.log (path.basename ('/foo/bar/baz/asdf/'));  // asdfconsole.log (path.basename (' c:/test/aaa '));  // AAA

"path.extname (p)"

Returns the extension of the path p, from the last '. ' To the end of the String. If the last section does not have '. ', or the path is '. ' begins, an empty string is returned

var path = require (' path '); console.log (path.extname ('/foo/bar/baz/asdf/a.txt '));  // . txtconsole.log (path.extname ('/foo/bar/baz/asdf/a.txt.b '));  // . bconsole.log (path.extname ('/foo/bar/baz/asdf/a. '));  //  . Console.log (path.extname (' c:/test/aaa/. '));  // 'console.log (path.extname (' c:/test/aaa '));  // "'

Separator

"path.sep"

Returns the file delimiter under the corresponding platform, win under ' \ ', *nix '/'

var path = require (' path '); console.log (path.sep);   // win under \,*nix for/console.log (' foo\\bar\\baz '. split (path.sep));  // [' foo ', ' Bar ', ' baz ']console.log (' foo/bar/baz '. split (path.sep));  // win next returns [' Foo/bar/baz '], but will return [' foo ', ' Bar ', ' Baz '] under the *nix system

"path.delimiter"

Returns the path delimiter under the corresponding platform, win under '; ', *nix ': '

var path = require (' path '//win under ";", *nix under ":"console.log (path.sep);  // win under the \,*nix for the/

Standardization

"path.normalize (p)"

Normalize the path to handle the redundant "..", ".", "/" characters. When multiple slashes are found, they are replaced by a Slash. Reserved when a slash is included at the end of the path. Windows system uses backslashes

varPath = require (' path '); console.log (path.normalize (' a/b/c/. /user/bin '));//A\b\user\binConsole.log (path.normalize (' a/b/c///. /user/bin/'));//a\b\user\bin\Console.log (path.normalize (' a/b/c/. /.. /user/bin '));//A\user\binConsole.log (path.normalize (' a/b/c/. /.////.. /user/bin/. '));//A\userConsole.log (path.normalize (' a/b/c/. /.. /user/bin/. /.. /‘));//a\Console.log (path.normalize (' A. /.. /user/bin/. /.. /‘));//.. \Console.log (path.normalize (' A. /.. /user/bin/. /.. /.. /.. /‘));//.. \.. \.. \Console.log (path.normalize ('./a/. /./user/bin/./'));//user\bin\

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

Combine multiple paths and convert to normalized paths

var path = require (' path '); console.log (path.join ('////./a ', ' b////c ', ' user/')); // \a\b\c\userconsole.log (path.join (' a ', '). /.. /', ' user/'); // .. \user\

Absolute and Relative

"path.resolve ([from ...], to)"

The absolute path from the source address to the destination to, similar to executing a series of CD commands in the shell

Path.resolve (' foo/bar ', '/tmp/file/', ' ... ', '/'. /subfile ')

Similar to:

CD foo/barcd/tmp/file/CD. CD a/.. /subfilepwd

[note] if a from or to parameter is an absolute path (such as ' e:/abc ', or a path that begins with "/"), the previous from parameter is ignored

var path = require (' path '); console.log (path.resolve ('. ', ' testfiles/. ', ' Trdlayer ')); // D:\project\trdLayerconsole.log (path.resolve (' ... ', ' testfiles ', ' a.txt ')); // D:\testFiles\a.txtconsole.log (path.resolve (' D: ', ' abc ', ' d:/a ')); // d:\aconsole.log (path.resolve (' abc ', ' Ok.gif ')); // D:\project\abc\ok.gif // D:\project\subfile

"path.isabsolute (path)"

Path is an absolute path (such as ' e:/abc '), or a path that begins with "/", both return true

var path = require (' path '); console.log (path.isabsolute ('. /testfiles/seclayer ')); // falseconsole.log (path.isabsolute ('./join.js ')); // falseconsole.log (path.isabsolute (' temp ')); // falseConsole.log (path.isabsolute ('/temp/. /..‘)); // trueconsole.log (path.isabsolute (' E:/GITHUB/NODEAPI/ABC/EFG ')); // trueconsole.log (path.isabsolute ('///temp123 ')); // true

"path.relative (from, to)"

Gets the relative path from the to to, which can be seen as the opposite implementation of the Path.resolve

Path.resolve (from, path.relative (from)) = = Path.resolve (to)
var path = require (' path '); console.log (path.relative (' c:\\\test ', ' c:\\\impl\\bbb ')); // .. \IMPL\BBBconsole.log (path.relative (' c:/test/aaa ', ' c:/bbb ')); // .. \.. \BBBconsole.log (path.relative (' c:/test/aaa ', ' d:/bbb ')); // D:\bbb

Path module of Nodejs

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.