url

用于URL的处理与解析

来源:http://nodejs.cn/api/url.html#url_url_strings_and_url_objects

引入: const url = require('url');      或 var url = require('url');

 


 

new URL(input[,base])

>input解析到base上创建一个新的URL对象

const { URL } = require('url');
const myURL = new URL('/foo', 'https://example.org/');
// https://example.org/foo

url.href
>获取及设置序列化的URL

url.origin
>获取只读URL的origin部分

url.protocol
>获取及设置

url.username
>获取及设置

url.password

>获取及设置password

 

url.host
>获取及设置URL的 host部分(hostname:port)

url.hostname
>获取及设置url的hostname

url.port
>获取及设置port

url.pathname

>获取及设置path部分

const { URL } = require('url');
const myURL = new URL('https://example.org/abc/xyz?123');
console.log(myURL.pathname);
  // 输出 /abc/xyz

myURL.pathname = '/abcdef';
console.log(myURL.href);
  // 输出 https://example.org/abcdef?123

url.search
>获取及设置

url.hash
>获取及设置hase部分
const { URL } = require('url');
const myURL = new URL('https://example.org/foo#bar');
console.log(myURL.hash);
  // 输出 #bar

myURL.hash = 'baz';
console.log(myURL.href);
  // 输出 https://example.org/foo#baz

url.toSring() url.toJSON() 返回序列化URL,值与url.href相同




posted @ 2018-08-10 23:32  wyr_blog  阅读(125)  评论(0)    收藏  举报