44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
const path=require('path');
|
|
var testPath='f:code/node/chenxuan.js'
|
|
|
|
// 以获取路径中的最后一部分,经常通过这个方法获取路径中的文件名
|
|
console.group("------basename------")
|
|
console.log(path.basename(testPath))
|
|
console.log(path.basename(testPath,'.js'))
|
|
console.groupEnd()
|
|
|
|
// 获取路径中除文件名以外的路径
|
|
console.group("dirname")
|
|
console.log(path.dirname(testPath))
|
|
console.groupEnd()
|
|
|
|
console.group("------join------")
|
|
// __dirname 表示当前js文件所处的目录
|
|
// join 拼接路径
|
|
// '…/'会抵消掉一层路径
|
|
console.log(path.join(__dirname))
|
|
console.log(path.join(__dirname,'/hehe1','/hehehe2','/hehehe3'))
|
|
console.log(path.join(__dirname,'/hehe1','../','/hehehe3'))
|
|
console.groupEnd()
|
|
|
|
console.group("------parse------")
|
|
// parse 将路径格式化为对象
|
|
const mypath=path.join(__dirname,'/hehe1','/hehe2','/hehe3.html');
|
|
console.log(path.parse(mypath))
|
|
console.groupEnd()
|
|
|
|
console.group("------format------")
|
|
// format 将对象化的路径格式化为路径
|
|
const mypath1=path.join(__dirname,'/hehe1','/hehe2','/hehe3.html');
|
|
const objPath=path.parse(mypath1)
|
|
console.log(path.format(objPath))
|
|
console.groupEnd()
|
|
|
|
console.group("------extname------")
|
|
// 获取路径对象的扩展名
|
|
const mypath2=path.join(__dirname,'/hehe1','/hehe2','/hehe3.html');
|
|
const extname=path.extname(mypath2)
|
|
console.log(extname)
|
|
console.groupEnd()
|
|
|