Node.js URL() 方法
'url' 模块提供了用于 URL 解析和解析的实用程序。 getter 和 setter 在类原型上实现 URL 对象的属性,并且 URL 类在全局对象上可用。
新的 URL() (在 v7.0.0、v6.13.0 中添加) 方法是 URL 模块的内置应用程序编程接口,它通过解析相对于基础的输入来创建新的 URL 对象。如果 base 作为字符串传递,它将被解析为等效于new URL(base) 。
句法:
new URL(input[, base])
可以使用以下方式访问“url”模块:
const url = require('url');
参数:此方法接受上面提到的两个参数,如下所述:
input < 字符串 > :它是用于解析绝对或相对输入URL的字符串类型的输入。如果输入是相对的,则需要基数,如果输入是绝对的,则忽略。
基础<字符串> | < URL > :它是字符串类型或URL的基本 URL,用于解析输入是否是绝对的。
返回值:它返回生成的新 URL 以及主机名、协议、路径名等数据数组。
示例 1:文件名:index.js
Javascript
// Node.js program to demonstrate the
// new URL() method
// Using require to access url module
const url = require('url');
const newUrl = new URL(
'https://geeksforgeeks.org/p/a/t/h?query=string#hash');
// url array in JSON Format
console.log(newUrl);
const myUR = url.parse(
'https://geeksforgeeks.org/:3000/p/a/t/h?query=string#hash');
console.log(myUR);
console.log(URL === require('url').URL);
const myURL1 = new URL(
{ toString: () => 'https://geeksforgeeks.org/' });
console.log(myURL1.href)
Javascript
// Node.js program to demonstrate the
// new URL() method
// Using require to access url module
const url = require('url');
const parseURL = url.parse(
'https://geeksforgeeks.org:3000/p/a/t/h?query=string#hash');
console.log("1 =>", parseURL)
// Prints parsed URL
const newUrl1 = new URL('https://gfg.com');
// prints https://xn--g6w251d/
console.log("2 =>", newUrl1.href)
const myURL = new URL('/alfa',
'https://akash.org/');
console.log("3 =>", myURL.href)
// https://akash.org/alfa
let newUrl3 = new URL('http://Gfg.com/',
'https://gfg.org/');
// Prints http://gfg.com/
console.log("4 =>", newUrl3.href)
newUrl4 = new URL('https://Gfg.com/',
'https://gfg.org/');
// Prints https://gfg.com/
console.log("5 =>", newUrl4.href)
newUrl5 = new URL('foo://Geekyworld.com/',
'https://geekyworld.org/');
// prints foo://Geekyworld.com/
console.log("6 =>", newUrl5.href)
newUrl6 = new URL('http:Akash.com/',
'https://akash.org/');
// prints http://akash.com/
console.log("7 =>", newUrl6.href)
newUrl10 = new URL('http:Chota.com/',
'https://bong.org/');
// prints http://bong.com/
console.log("8 =>", newUrl10.href)
newUrl7 = new URL('https:Chota.com/',
'https://bong.org/');
// prints https://bong.org/Chota.com/
console.log("9 =>", newUrl7.href)
newUrl8 = new URL('foo:ALfa.com/',
'https://alfa.org/');
// Prints foo:ALfa.com/
console.log("10 =>", newUrl8.href)
输出:
URL {
href: ‘https://geeksforgeeks.org/p/a/t/h?query=string#hash’,
origin: ‘https://geeksforgeeks.org’,
protocol: ‘https:’,
username: ”,
password: ”,
host: ‘geeksforgeeks.org’,
hostname: ‘geeksforgeeks.org’,
port: ”,
pathname: ‘/p/a/t/h’,
search: ‘?query=string’,
searchParams: URLSearchParams { ‘query’ => ‘string’ },
hash: ‘#hash’
}
Url {
protocol: ‘https:’,
slashes: true,
auth: null,
host: ‘geeksforgeeks.org’,
port: null,
hostname: ‘geeksforgeeks.org’,
hash: ‘#hash’,
search: ‘?query=string’,
query: ‘query=string’,
pathname: ‘/:3000/p/a/t/h’,
path: ‘/:3000/p/a/t/h?query=string’,
href: ‘https://geeksforgeeks.org/:3000/p/a/t/h?query=string#hash’
}
true
https://geeksforgeeks.org/
示例 2:文件名:index.js
Javascript
// Node.js program to demonstrate the
// new URL() method
// Using require to access url module
const url = require('url');
const parseURL = url.parse(
'https://geeksforgeeks.org:3000/p/a/t/h?query=string#hash');
console.log("1 =>", parseURL)
// Prints parsed URL
const newUrl1 = new URL('https://gfg.com');
// prints https://xn--g6w251d/
console.log("2 =>", newUrl1.href)
const myURL = new URL('/alfa',
'https://akash.org/');
console.log("3 =>", myURL.href)
// https://akash.org/alfa
let newUrl3 = new URL('http://Gfg.com/',
'https://gfg.org/');
// Prints http://gfg.com/
console.log("4 =>", newUrl3.href)
newUrl4 = new URL('https://Gfg.com/',
'https://gfg.org/');
// Prints https://gfg.com/
console.log("5 =>", newUrl4.href)
newUrl5 = new URL('foo://Geekyworld.com/',
'https://geekyworld.org/');
// prints foo://Geekyworld.com/
console.log("6 =>", newUrl5.href)
newUrl6 = new URL('http:Akash.com/',
'https://akash.org/');
// prints http://akash.com/
console.log("7 =>", newUrl6.href)
newUrl10 = new URL('http:Chota.com/',
'https://bong.org/');
// prints http://bong.com/
console.log("8 =>", newUrl10.href)
newUrl7 = new URL('https:Chota.com/',
'https://bong.org/');
// prints https://bong.org/Chota.com/
console.log("9 =>", newUrl7.href)
newUrl8 = new URL('foo:ALfa.com/',
'https://alfa.org/');
// Prints foo:ALfa.com/
console.log("10 =>", newUrl8.href)
使用以下命令运行index.js文件:
node index.js
输出:
1 => Url {
protocol: ‘https:’,
slashes: true,
auth: null,
host: ‘geeksforgeeks.org:3000’,
port: ‘3000’,
hostname: ‘geeksforgeeks.org’,
hash: ‘#hash’,
search: ‘?query=string’,
query: ‘query=string’,
pathname: ‘/p/a/t/h’,
path: ‘/p/a/t/h?query=string’,
href: ‘https://geeksforgeeks.org:3000/p/a/t/h?query=string#hash’
}
2 => https://gfg.com/
3 => https://akash.org/alfa
4 => http://gfg.com/
5 => https://gfg.com/
6 => foo://Geekyworld.com/
7 => http://akash.com/
8 => http://chota.com/
9 => https://bong.org/Chota.com/
10 => foo:ALfa.com/
参考: https://nodejs.org/api/url.html#url_new_url_input_base