• Node.js Video Tutorials

NodeJS urlObject.pathname 屬性



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 的路徑段中檢索路徑名部分。

示例

在下面的示例中,我們嘗試從提供的 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

示例

如果提供的 URL 未由 parse() 方法解析,則 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
nodejs_url_module.htm
廣告