借助於urlObject.pathname()方法,我們可以找到給定主機名使用的路徑的名稱。它包含從主機(帶有端口)開始,在查詢或哈希組件開始之前的所有內容,這些內容由ASCII問號(?)或哈希(#)字符之一分隔。
用法: urlObject.pathname() 返回:Returns the pathname used(i.e.'/p/a/t/h')
範例1:
const url = require('url');
var address =
'https://u:p@www.example.com:777/a/b?c=d&e=f#g';
// Parse the address:
var q = url.parse(address, true);
/* The parse method returns an object containing
URL properties */
console.log(q.pathname);
輸出:
/a/b
範例2:
const url = require('url');
var address =
'http://example.com/';
// Parse the address:
var q = url.parse(address, true);
/* The parse method returns an object containing
URL properties */
console.log(q.pathname);
輸出:
/
支持的瀏覽器:
- 穀歌瀏覽器
- IE
- Edge
- Opera
- 蘋果Safari
相關用法
- Node.js URL.pathToFileURL用法及代碼示例
- Node.js URL.fileURLToPath用法及代碼示例
- Node.js URLSearchParams.has()用法及代碼示例
- Node.js URL.href用法及代碼示例
- Node.js URL.password用法及代碼示例
注:本文由純淨天空篩選整理自DeepakDev大神的英文原創作品 Node | urlObject.pathname API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。