废话不多说,开始。
如果用到了path这个模块首先得引入
const path=require('path')
console.log(path)
//返回
{ resolve: [Function: resolve],
normalize: [Function: normalize],
isAbsolute: [Function: isAbsolute],
join: [Function: join],
relative: [Function: relative],
_makeLong: [Function: _makeLong],
dirname: [Function: dirname],
basename: [Function: basename],
extname: [Function: extname],
format: [Function: format],
parse: [Function: parse],
sep: '\\',
delimiter: ';',
win32: [Circular],
posix:
{ resolve: [Function: resolve],
normalize: [Function: normalize],
isAbsolute: [Function: isAbsolute],
join: [Function: join],
relative: [Function: relative],
_makeLong: [Function: _makeLong],
dirname: [Function: dirname],
basename: [Function: basename],
extname: [Function: extname],
format: [Function: format],
parse: [Function: parse],
sep: '/',
delimiter: ':',
win32: [Circular],
posix: [Circular] } }
1、path.basename('path','[,ext]')返回路径的最后一部分,后面的目录分割符被忽略了。注:一定是下面的格式(是反斜线,不是斜线)
console.log(path.basename('e:/wwwroot/newshop.busionline.com/composer.json'))
//返回
composer.json
console.log(path.basename('e:/wwwroot/newshop.busionline.com/composer.json','.json'))
//返回
composer
2、path.delimiter 路径分隔符
//先找点路径
console.log(process.env.PATH);
; for Windows
: for POSIX
//windows 的返回值
D:\appproject\Ruby24-x64\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT
//POSIX 的返回值
D:\appproject\Ruby24-x64\bin:C:\ProgramData\Oracle\Java\javapath:C:\Program Files (x86)\Intel\iCLS Client\:C:\Program Files\Intel\iCLS Client\:C:\Windows\system32:C:\Windows:C:\Windows\System32\Wbem:C:\Windows\System32\WindowsPowerShell\v1.0\:C:\Program Files\Intel\Intel(R) Management Engine Components\DAL:C:\Program Files\Intel\Intel(R) Management Engine Components\IPT:C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL:C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT
console.log(process.env.PATH.split(path.delimiter))
//windows 返回值
[ 'D:\\appproject\\Ruby24-x64\\bin',
'C:\\ProgramData\\Oracle\\Java\\javapath',
'C:\\Program Files (x86)\\Intel\\iCLS Client\\',
'C:\\Program Files\\Intel\\iCLS Client\\',
'C:\\Windows\\system32',
'C:\\Windows',
'C:\\Windows\\System32\\Wbem',
'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\',
'C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL',
'C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT',
'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL',
'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT' ]
//POSIX 返回值
[ 'D:/appproject/Ruby24-x64/bin',
'C:/ProgramData/Oracle/Java/javapath',
'C:/Program Files (x86)/Intel/iCLS Client/',
'C:/Program Files/Intel/iCLS Client/',
'C:/Windows/system32',
'C:/Windows',
'C:/Windows/System32/Wbem',
'C:/Windows/System32/WindowsPowerShell/v1.0/',
'C:/Program Files/Intel/Intel(R) Management Engine Components/DAL',
'C:/Program Files/Intel/Intel(R) Management Engine Components/IPT',
'C:/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL',
'C:/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT' ]
3、path.dirname(path) 返回路径目录,尾部的目录分隔符被忽略了
console.log(path.dirname('e:/wwwroot/newshop.busionline.com/composer.json'))
//返回
e:/wwwroot/newshop.busionline.com
4、path.extname(path) 返回路径的扩展部分(路径的最后一部分(从点最后一次出现的位置到路径结束),如果在最后一部分没有点或者 path.basename() 是点则返回的是空)
1、console.log(path.extname('e:/wwwroot/newshop.busionline.com/composer.json'))
//返回
.json
2、console.log(path.extname('e:/wwwroot/newshop.busionline.com/.gitignore'))
//返回
''(空)
3、console.log(path.extname('e:/wwwroot/newshop.busionline.com'))
//返回
.com
4、console.log(path.extname('e:/wwwroot/newshop.busionline.com/index.'))
//返回
.
5、console.log(path.extname('e:/wwwroot/newshop.busionline.com/index'))
//返回
''(空)
5、path.format(pathObject) 把路径对象转成路径字符串
6、path.isAbsolute(path) 确定路径是否为绝对路径 (如果路径长度为0,则返回false)(不会对路径是否真实存在进行判断)
console.log((path.isAbsolute('e:/wwwroot')))
//返回
true
console.log(path.isAbsolute('e:/wwwroot/newshop.busionline.com/composer.json'))
//返回
true
console.log(path.isAbsolute('e')))
//返回
false
console.log(path.isAbsolute('D:/aaa')) (我的d盘中无此文件夹)
//返回
true
console.log(path.isAbsolute('D:/sdk')) (我的d盘中有此文件夹)
//返回
true
console.log(path.isAbsolute('//server'));
//返回
true
console.log(path.isAbsolute('\\\\server'));
//返回
true
console.log(path.isAbsolute('C:/foo/..'));
//返回
true
console.log(path.isAbsolute('C:\\foo\\..'));
//返回
true
console.log(path.isAbsolute('bar\\baz'));
//返回
false
console.log(path.isAbsolute('bar/baz'));
// 返回
false
console.log(path.isAbsolute('.'));
// 返回
false
7、path.join([...paths]) 使用平台特定的分隔符将所有给定的路径连接在一起,然后将结果路径规范化(如果连接路径长度为0则返回“.”(点),表示当前工作目录)(连接路径类型必须为字符串型)
console.log(path.join('.','app','ccc','foot'))
//返回
app\ccc\foot
console.log(path.join('../','app','ccc/','/foot','/'))
//返回
..\app\ccc\foot\
console.log(path.join('../','app','./','/foot','/'))
//返回
..\app\foot\
8、path.normalize(path) 格式化路径,将路径规范化
console.log(path.normalize('../index//app///css/head'))
//返回
..\index\app\css\head
console.log(path.normalize('../index//app///css/head..//'))
//返回
..\index\app\css\head..\
console.log(path.normalize('index//app///css/head..'))
//返回
index\app\css\head..
9、path.parse(path) 返回一个对象(属性表示路径重要元素)
console.log(path.parse('https://mp.csdn.net/postedit/81982824'))
//返回
{ root: '',
dir: 'https://mp.csdn.net/postedit',
base: '81982824',
ext: '',
name: '81982824' }
console.log(path.parse('/https://mp.csdn.net/postedit/81982824'))
//返回
{ root: '/',
dir: 'https://mp.csdn.net/postedit',
base: '81982824',
ext: '',
name: '81982824' }
11、path.relative(from, to) 基于当前工作目录返回相对路径如果每次解析到同一路径(在对每个路径调用path.false()之后),则返回一个零长度字符串。
console.log(path.relative('./cc/abbs/hhh','./bbb/sss'))
//返回
..\..\..\bbb\sss
console.log(path.relative('./cc/abbs/hhh','./cc/bbb/sss'))
//返回
..\..\bbb\sss
console.log(path.relative('/cc/abbs/hhh','/aa/bbb/sss'))
//返回
..\..\..\aa\bbb\sss
console.log(path.relative('wwwto','/cc/abbs/','/aa/bbb/sss',))
//返回
..\..\..\..\cc\abbs
console.log(path.relative('/cc/abbs/','/aa/bbb/sss','wwwto'))
//返回
..\..\aa\bbb\sss
12、path.resolve([...paths]) 将一个路径数组(序列)解析为绝对路径
console.log(path.resolve('/cc/abbs/','/aa/bbb/sss','wwwto'))
//返回
e:\aa\bbb\sss\wwwto
console.log(path.resolve('/cc/abbs/','./bbb/sss','wwwto'))
//返回
e:\cc\abbs\bbb\sss\wwwto
console.log(path.resolve('./cc/abbs/hhh','./bbb/sss','wwwto'))
//返回
e:\learn\node\node的模块\cc\abbs\hhh\bbb\sss\wwwto
console.log(path.resolve('wwwto','../cc/abbs/hhh','./bbb/sss'))
//返回
e:\learn\node\node的模块\cc\abbs\hhh\bbb\sss
13、path.sep 提供特定平台的路径分隔符
var str = 'https://mp.csdn.net/postedit/81982824'
console.log(str.split(path.sep))
//返回
[ 'https://mp.csdn.net/postedit/81982824' ]
var str = '../aa/ddd/d///fg/f'
console.log(str.split(path.sep))
//返回
[ '../aa/ddd/d///fg/f' ]