node的核心模块path

时间:2023-03-09 18:01:40
node的核心模块path
//导入模块path
var path=require("path"); //path.basename :输出文件名+后缀
//console.log(path.basename("\shen\node\path\123.html")); \写反了
console.log(path.basename("shen/node/path/123.html"));//123.html
console.log(path.basename("shen/node/path/123"));// //dirname()获取目录
console.log(path.dirname("shen/node/path"));// node/02-node第二天 //extname()获取文件的扩展名
console.log(path.extname("shen/node/path/123.html"));//.html
console.log(path.extname("shen/node/path/123"));//空 //join()合并路径
var p1="node/path1/lala";
var p2="node/path2/lili";
console.log(path.join(p1,p2));//node\path1\lala\node\path2\lili //parse()把路径转换为一个对象
console.log(path.parse("c:/shen/node/path/123.html"));
//{ root: 'c:/',
// dir: 'c:/shen/node/path',
// base: '123.html',
// ext: '.html',
// name: '123' } //format()把一个路径对象转换成一个路径字符串
var obj={ root: 'c:/',
dir: 'c:/shen/node/path',
base: '123.html',
ext: '.html',
name: '123' }
console.log(path.format(obj));
//c:/shen/node/path\123.html //isAbsolute判断是否在根路径上
console.log(path.isAbsolute("c:/shen/node/path\123.html"));//true
console.log(path.isAbsolute("node/path\123.html"));//false
console.log(path.isAbsolute("/node/path\123.html"));//true