urlObject 的 NodeJS urlObject.pathname 属性指定 URL 的路径段中的路径名部分。此属性不考虑搜索段,即使它存在于 URL 中也是如此。
在 URL 中,路径段位于端口段和查询段或哈希段之间,查询段或哈希段由 ASCII 问号 (?) 或哈希 (#) 字符分隔。
例如,考虑此 URL“https://user:pass@site.com:80/pa/th?que=sea#hash”。
- “/pa/th?que=sea”是路径段。
- “/pa/th”是路径名段。
语法
以下是 NodeJS urlObject.pathname 属性的语法
urlObject.pathname
参数
此属性不接受任何参数。
返回值
此属性从 URL 的 path 段中检索路径名部分。
例在以下示例中,我们尝试从提供的 URL 中的路径段获取路径名部分。
const url = require('url');
let address = 'https://user:pass@site.com:80000/pa/th?query=search#hash';
let result = url.parse(address, true);
console.log(result.pathname);
输出
正如我们在下面的输出中看到的,NodeJS pathname 属性返回了整个路径名部分。
/pa/th
例
如果 parse() 方法未解析提供的 URL,则 pathname 属性将检索 undefined。
在以下示例中,我们不分析 URL 字符串。
const url = require('url');
let address = 'https://user:pass@site.com:80000/pa/th?query=search#hash';
console.log(address.pathname);
输出
以下是上述代码的输出
undefined