1.获取包含后缀名的文件名
const path = require('path')
const basename = path.basename('C:/list/win-i386/obsolete/index.html')
console.log(basename);
const basename1 = path.basename('C:/list/win-i386/obsolete/index.html','.html')
console.log(basename1);
2.获取目录名
const path = require('path')
const dirname = path.dirname('C:/list/win-i386/obsolete/index.html')
console.log(dirname);
3.获取后缀名
const path = require('path')
const extname = path.extname('C:/list/win-i386/obsolete/index.html')
console.log(extname);
4.获取文件的根目录 目录(包含后缀名)文件名 后缀名(不包含)文件名
const path = require('path')
const parse = path.parse('C:/list/win-i386/obsolete/index.html')
console.log(parse);
5.判断是否是绝对路径
const path = require('path')
const isAbsolute = path.isAbsolute('C:/list/win-i386/obsolete/index.html')
console.log(isAbsolute);
6.拼接路径
const path = require('path')
const join = path.join('C:/list/win-i386/obsolete/','index.html')
console.log(join);
7. __dirname和__filename
在node中 ./ 表示的是针对于命令行最后的一个目录来说,
而不是针对与当前的文件来说的,require()方法的导入自己定义的模块中的./则不受影响
- __dirname
当前目录的绝对路径,是动态变化的 - __filename
当前文件的绝对路径,是动态变化的 - 例如 C:/list/win-i386/obsolete/index.html
__dirname: C:/list/win-i386/obsolete
__filename: C:/list/win-i386/obsolete/index.html