📜  Node.js urlObject.path 属性

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

Node.js urlObject.path 属性

在本文中,我们将学习 urlObject.path API。 urlObject.path API属性是路径名和搜索组件的串联。路径名是指 URL 中的文件路径,搜索组件是指具有固定限制的查询和哈希字符串,例如问号 (?) 或哈希 (#)字符。不执行路径解码。

示例:让我们考虑一个 URL 'http://user:pass@sub.example.com:8080/p/a/t/h?query= 字符串 #hash'

Syntax: urlObject.path
Return: '/p/a/t/h?query=string'

urlObject.path 在 url.parse()函数返回后返回 '/p/a/t/h?query= 字符串 '。

示例 1:Index.js

Javascript
//Importing url module
const http = require('url'); 
     
// Creating a demo URL 
const myURL = 
'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'; 
  
// Parsing the Address
var q = http.parse(myURL, true);    
  
// Displaying the path  
console.log(q.path);


Javascript
//Importing the url module
const http = require('url'); 
     
// Creating a demo URL 
const myURL = 
'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'; 
  
var q = http.parse(myURL, true);    
  
// Display path value of myURL before change 
console.log("Before Change");  
console.log(q.path);    
console.log(); 
  
// Changing the path
q.path='/s/k/t/j?query=abc@gmail.com' 
  
// Printing the changed path
console.log("After Change"); 
console.log(q.path);


执行命令:

node index.js

控制台输出:

/p/a/t/h?query=string

示例2:(更改路径)

Javascript

//Importing the url module
const http = require('url'); 
     
// Creating a demo URL 
const myURL = 
'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'; 
  
var q = http.parse(myURL, true);    
  
// Display path value of myURL before change 
console.log("Before Change");  
console.log(q.path);    
console.log(); 
  
// Changing the path
q.path='/s/k/t/j?query=abc@gmail.com' 
  
// Printing the changed path
console.log("After Change"); 
console.log(q.path);

执行命令:

node index.js

控制台输出:

Before Change
/p/a/t/h?query=string

After Change
/s/k/t/j?query=abc@gmail.com