path 模塊
path 模塊提供了 操作路徑 的功能,如下是幾個較為常用的幾個 API:
- 代碼實例:
const path = require('path');//獲取路徑分隔符
console.log(path.sep);//拼接絕對路徑
console.log(path.resolve(__dirname, 'test'));//解析路徑
let pathname = 'D:/program file/nodejs/node.exe';
console.log(path.parse(pathname));//獲取路徑基礎名稱
console.log(path.basename(pathname))//獲取路徑的目錄名
console.log(path.dirname(pathname));//獲取路徑的擴展名
console.log(path.extname(pathname));