1. 程式人生 > >node.js中的path;

node.js中的path;

/**
 * Created by SlzStar on 2017/9/18.
 */
//操作路徑;
var path = require("path");
//定義檔案路徑;
var newPath = "/user/index.html";
/*
    返回路徑字串的物件;path.parse();
     { root: '/',
     dir: '/user',
     base: 'index.html',
     ext: '.html',
     name: 'index' }
 */
console.log(path.parse(newPath));


//獲取路徑名;path.dirname();
console.log(path.dirname(newPath))  //  /user
//獲取拓展名;path.extname()
console.log(path.extname(newPath))  // ./html
//獲取完整檔名;
console.log(path.basename(newPath))  //index.html
//用於連線路徑;path.join();
console.log(path.join("./", "/user", "./index.html")); //user\index.html
//用於將相對路徑轉換為絕對路徑;從一個引數的路徑到另一個引數的路徑,該怎麼走;
console.log(path.relative("./user/index.html", "./use/list.html")); //..\..\use\list.html
//將to引數解析為絕對路徑;path.resolve([from ...], to);
console.log(path.resolve("./index.html")) //D:\webStorm-file\9-18-sunlizhen\index.html
//判斷引數是否是絕對路徑;isAbolute(path); 是返回true  不是返回false
console.log(path.isAbsolute(newPath));  //true
//從物件中返回路徑字元創,與path.parse相反;path.format();
var user = {
    dir : './user/index',
    ext : '.html',
    base : 'index.html'
}
console.log(path.format(user)); //   ./user/index\index.html