path module
// two kinds of delimiters windows path: C: \ the TEMP \ myfile.html mac, Unix, Linux path: /tmp/myfile.html // website address is / a / b / c / d
basename
// base is substantially in the basename ( "path string", "optional expansion name"); Returns the filename is a string argument const path = require ( 'path' ); // introduction path using the path to module path.basename ( '/ a / b.html'); // returns: 'b.html' name with the suffix path.basename ( '. HTML' '/ a / b.html',); // not B with the suffix // path \ a \ b.html the console.log (path.basename ( '\\ \\ b.html a')); // returns b.html since \ escaped the effect of the presence of, generally use \\ / left -> forward slash, \ Right -> backslash
dirname
path.dirname ( "Path String"); Returns the last file in the directory is to return the last preceding path var S = path.dirname ( "/ A / B / C / D"); var path.dirname SS = ( "/a/b/c/d/e.js"); / A / B / C / A / B / C / D
extname
// extension path.extname ( "Path string") Returns the last occurrence. (Period) character string to the end of the last part of the path var ss = path.extname ( "/ a / b / c / d / e .js "); //.js the console.log (path.extname (" / A / B / C / D / e.js.css ")); //.css
resolve
path.resolve () method of some of the route / path segments resolved to an absolute path. Example: my current working directory is / Workspace / Demo path.resolve () = absolute path >> / workspace / demo the current working directory path.resolve ( '') = >> / workspace / demo path.resolve (__ dirname) >> = / Workspace / Demo path.resolve ( '/ IMG / Books', '/ NET') >> = '/ NET' last / root directory path.resolve ( 'img / books', '/ net' ) >> = '/ NET' path.resolve ( 'IMG / Books', './net') >> = '/ Workspace / Demo / IMG / Books / NET' IMG no preceding / following default is the current path. / last directory path on the representative path.resolve ( '/ IMG / Books',' ./net ') >> =' / IMG / Books / NET ' path.resolve (' / IMG / Books', 'NET ') = >>' / img / books / net ' without / with plus. path.resolve (' the src ','. / IMG / Books', '.. / NET ') >> =' / Workspace / Demo / the src / IMG / NET ' path.resolve (' the src ',' IMG / Books ',' ../net ') >> =' / Workspace / Demo / the src / img / NET ' // sum up: from back to front, when the characters begin with / without stitching to the front of the path; // in terms beginning ../ in front of the path of joining, but in front of a free last layer path; // beginning ./ In terms of the splicing or no symbol preceding path; ./ same folder
join
Path segment is equal to zero is ignored. If the path is connected to a string of zero length string is returned, indicating the current working directory '' length of zero is the empty string is ""; path.join ( '/ IMG', 'Book' , 'NET / ABC', 'Inter', '..'); = >> / IMG / Book = >> / IMG / Book / NET / >> = ABC / IMG / Book / NET / ABC / Inter => > / IMG / Book / NET / ABC path.join ( '/ IMG / Books',' ../net ')) = >> / IMG / NET with ../ .. returned last directory path.join (' img / books ',' ../net ' )) = >> img / img front of the net is not / will not add the current directory this is the difference with path.resolve path.join (' / img / books ' ,'. / net ')) = >> / img / books / net ./ with no / or. the same path.join (' IMG / Books', './net')) >> = IMG / Books / NET path .join ( '/ img / books' , 'net')) = >> // sum up the difference: join () just stitching each path segment, // not as resolve () as the various fields in addition to stitching stitching also the path of the working directory, // followed if / beginning of the string and join fragments unlike resolve itself as only return, // same ../ .. there is a sense represents one level
parse
path.parse () method returns an object that is part of the path object. var s = path.parse ( "/ a / b / c / d / e.js"); return an object {root: '/', dir : '/ a / b / c / d', base: 'e .js', ext: '.js' , name:' e '}
format
path.format (a passing object that is returned by the object path.parse) Returns a string path var S = path.format ({ the root: "\ R & lt", the dir: "A B \\ C \\" , Base: "e.js" }); A \ B \ C \ e.js
sep
var s = path.sep; without returning the same control for different console.log (s); return \ returns the delimiter between folders