當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js URL.pathname用法及代碼示例

url.pathname是URL類的內置應用程序編程接口,帶有in url模塊,該模塊用於獲取和設置URL的路徑名部分。

const url.pathname

返回值:它獲取並設置URL的路徑名部分。

以下示例程序旨在說明url.pathname方法的使用:


範例1:

// node program to demonstrate the  
// url.pathname API as Setter  
   
//importing the module 'url' 
const http = require('url'); 
   
// creating and initializing myURL 
const myURL = new URL('https://example.com:80/foo#ram'); 
   
// Display the href 
// value of myURL before change 
console.log("Before Change"); 
console.log(myURL.href); 
   
// assinging pathname portion 
// using pathname API 
console.log(); 
myURL.pathname = '/abcdef'; 
   
// Display href  
// value of myURL after change 
console.log("After Change"); 
console.log(myURL.href);

輸出

Before Change
https://example.com:80/foo#ram

After Change
https://example.com:80/abcdef#ram

範例2:

// node program to demonstrate the  
// url.pathname API as Getter  
  
//importing the module 'url' 
const http = require('url'); 
  
// creating and initializing myURL 
const myURL = new URL('https://example.org/123abc#ram'); 
  
// getting the pathname portion 
// using pathname 
const pathname = myURL.pathname; 
  
// Display pathname value  
console.log("pathname is:" + pathname);

輸出:

pathname is:/123abc

注意:以上程序將通過在Node上使用node myapp.js命令進行編譯和運行。

參考:
https://nodejs.org/api/url.html#url_url_pathname



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node | URL.pathname API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。