📜  节点 | URL.pathname API

📅  最后修改于: 2022-05-13 01:56:57.272000             🧑  作者: Mango

节点 | URL.pathname API

url.pathname是类 URL的内置应用程序编程接口,带有 in url模块,用于获取和设置 URL 的路径名部分。

句法:

const url.pathname

返回值:它获取和设置 URL 的路径名部分。

下面的程序说明了url.pathname方法的使用:

示例 1:



Javascript
// 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);
  
// assigning 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);


Javascript
// 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);


输出

示例 2:

Javascript

// 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);

输出:

注意:上面的程序将在 Node.js 上使用 node myapp.js 命令编译和运行。

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