urlObject 的 NodeJS urlObject.path 属性是路径名和搜索部分的组合。此属性指定路径段以及搜索部分(如果存在)。此属性不执行路径段的解码。
例如,考虑一个 URL“https://user:pass@site.com:80000/ 例?q=pas#hash”。
- “/例?q=pas”是路径段。
- “/例”是路径名部分。
- “?q=pas”是搜索部分。
路径段包含一个字符“例”,如果被解码,则值将为“xn--fsq/”。由于 NodeJs path 属性不执行解码,因此返回的值将为 “https://user:pass@Site.com/ 例#hash”。
语法
以下是 NodeJS urlObject.path 属性的语法
urlObject.path
参数
此属性不接受任何参数。
返回值
此属性检索 URL 的路径段以及搜索部分(如果存在)。
例如果指定的 URL 包含路径段,则 NodeJS 路径属性将检索该段。
在下面的示例中,我们尝试从指定的 URL 获取路径段。
const url = require('url');
let address = 'https://user:pass@site.com:80000/pa/th#hashh';
let result = url.parse(address, true);
console.log(result.path);
输出
正如我们在下面的输出中看到的,NodeJS path 属性从 URL 中检索了路径段。
/pa/th
例
如果提供的 URL 包含路径段,则路径属性将检索该段,包括搜索部分(如果存在)。
在以下示例中,我们还包含搜索部分以及 URL 的路径段。
const url = require('url');
let address = 'https://user:pass@site.com:80000/pa/th?q=val#hashh';
let result = url.parse(address, true);
console.log(result.path);
输出
以下是上述代码的输出
path 属性将检索路径段以及给定 URL 中存在的搜索部分。
/pa/th?q=val
例
如果未使用 NodeJS parse() 方法解析提供的 URL 字符串,则 NodeJS 路径属性将未定义。
在以下示例中,我们不分析 URL 字符串。
const url = require('url');
let address = 'https://user:pass@site.com:8000/pa/th?q=val#hashh';
console.log(address.path);
输出
以下是上述代码的输出
undefined