一、url.parse(url,true,true)利用WHATWG API解析一个URL字符串:
url.parse('http://www.abc.com/index/abc/index.html?search=123&bach=1234#hash')
生成
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.abc.com',
port: null,
hostname: 'www.abc.com',
hash: '#hash',
search: '?search=123&bach=1234',
query: 'search=123&bach=1234',
pathname: '/index/abc/index.html',
path: '/index/abc/index.html?search=123&bach=1234',
href: 'http://www.abc.com/index/abc/index.html?search=123&bach=1234#hash' }
url.parse('//www.abc.com/index/abc/index.html?search=123&bach=1234#hash')
生成
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: '#hash',
search: '?search=123&bach=1234',
query: 'search=123&bach=1234',
pathname: '//www.abc.com/index/abc/index.html',
path: '//www.abc.com/index/abc/index.html?search=123&bach=1234',
href: '//www.abc.com/index/abc/index.html?search=123&bach=1234#hash' }
url.parse('//www.abc.com/index/abc/index.html?search=123&bach=1234#hash',true,true)
Url {
protocol: null,
slashes: true,
auth: null,
host: 'www.abc.com',
port: null,
hostname: 'www.abc.com',
hash: '#hash',
search: '?search=123&bach=1234',
query: { search: '123', bach: '1234' },
pathname: '/index/abc/index.html',
path: '/index/abc/index.html?search=123&bach=1234',
href: '//www.abc.com/index/abc/index.html?search=123&bach=1234#hash' }
二、url.format({})返回一个WHATWG URL对象的可自定义序列化的URL字符串表达。
url.format({
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.imooc.com',
port: null,
hostname: 'www.imooc.com',
hash: '#333',
search: '?from=scott&course=123',
query: 'from=scott&course=123',
pathname: '/video/6710',
path: '/video/6710?from=scott&course=123',
href: 'http://www.imooc.com/video/6710?from=scott&course=123#333' })
生成
http://www.imooc.com/video/6710?from=scott&course=123#333
三、url.resolve(from, to)Web 浏览器解析超链接的方式把一个目标 URL 解析成相对于一个基础 URL。
url.resolve('http://abc/123/', '/four/456')
生成
'http://abc/four/456'