path 模块的 Node.js path.parse() 方法将给定路径的属性列为对象。以下是表示给定路径的对象属性。
- dir - 此属性指定给定路径的目录名称。
- root − 此属性指定给定路径的根名称。
- base − 此属性指定具有给定路径扩展名的文件名。
- name − 此属性指定给定路径的文件名。
- ext − 此属性仅指定给定路径的扩展。
返回对象属性的值对于不同的平台(如 POSIX)可能会有所不同。此方法忽略尾随目录分隔符。
语法
以下是 path 模块的 path.parse() 方法Node.js语法。
path.parse(path)
参数
path − 此参数指定方法将要解析的路径。如果路径不是字符串,则会抛出 TypeError。
返回值
此方法返回一个包含属性的对象,这些属性指定给定路径的段。
例如果将完全规范化的路径传递给此方法,则表示给定路径的所有属性都将作为对象返回。
在以下示例中,我们将为该方法提供 'root'、'dir'、'base'、'ext' 和 'name' 的所有属性。
const path = require('path');
var result = path.parse("C:/Users/Lenovo/Desktop/JavaScript/nodefile.js");
console.log(result);
输出
在在线编译器(POSIX)中执行上述程序后,该方法将所有路径属性作为对象返回。
{
root: '',
dir: 'C:/Users/Lenovo/Desktop/JavaScript',
base: 'nodefile.js',
ext: '.js',
name: 'nodefile'
}
root: '',
dir: 'C:/Users/Lenovo/Desktop/JavaScript',
base: 'nodefile.js',
ext: '.js',
name: 'nodefile'
}
以下是我们在 WINDOWS 操作系统上执行上述代码时的输出。
{
root: 'C:/',
dir: 'C:/Users/Lenovo/Desktop/JavaScript',
base: 'nodefile.js',
ext: '.js',
name: 'nodefile'
}
root: 'C:/',
dir: 'C:/Users/Lenovo/Desktop/JavaScript',
base: 'nodefile.js',
ext: '.js',
name: 'nodefile'
}
例
如果将空路径传递给此方法,则返回对象的属性将为空。
在以下示例中,我们将一个空路径传递给 path.parse() 方法。
const path = require('path');
var result = path.parse("");
console.log(result);
输出
执行上述程序后,path.parse() 方法返回空对象属性。
{ root: '', dir: '', base: '', ext: '', name: '' }
例
如果未将路径的任何路径段提供给此方法,则返回对象的特定路径段属性将为空。
在给定的程序中,我们尝试传递没有根段的路径。
const path = require('path');
var result = path.parse("JavaScript/nodefile.js");
console.log(result);
输出
如果我们编译并运行上述程序,该方法会将“root”属性返回为空,因为给定路径中没有给出“root”段。
{
root: '',
dir: 'JavaScript',
base: 'nodefile.js',
ext: '.js',
name: 'nodefile'
}
root: '',
dir: 'JavaScript',
base: 'nodefile.js',
ext: '.js',
name: 'nodefile'
}
例
如果我们将一个不是字符串类型的值传递给 path 参数,此方法将抛出 TypeError。
在给定的示例中,我们将对象的实例而不是字符串传递给方法的路径参数。
const path = require('path');
var result = path.parse({});
console.log(result);
类型错误
执行上述程序后,path.isAbsolute() 方法会抛出 TypeError,因为 path 参数不是字符串值。
path.js:39
throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
at assertPath (path.js:39:11)
at Object.parse (path.js:1436:5)
at Object.<anonymous> (/home/cg/root/63a02f1595a95/main.js:3:19)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)at startup (internal/bootstrap/node.js:238:19)
throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
at assertPath (path.js:39:11)
at Object.parse (path.js:1436:5)
at Object.<anonymous> (/home/cg/root/63a02f1595a95/main.js:3:19)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)at startup (internal/bootstrap/node.js:238:19)