URL网址解析
[root@localhost ~]# node
> url
{ parse: [Function: urlParse],
resolve: [Function: urlResolve],
resolveObject: [Function: urlResolveObject],
format: [Function: urlFormat],
Url: [Function: Url] }
parse
url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
- urlString:<String> The URL string to parse.
- parseQueryString:<Boolean> If true, the query property will always be set to an object returned by the querystring module's parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. Defaults to false.
- slashesDenoteHost:<Boolean> If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: 'foo', pathname: '/bar'} rather than {pathname: '//foo/bar'}. Defaults to false.
【实例】
> url.parse('https://www.google.com/?gws_rd=ssl')
Url {
protocol: 'https:',//协议
slashes: true, //是否有协议的双斜线
auth: null,
host: 'www.google.com', //ip地址,域名
port: null, //端口
hostname: 'www.google.com', //主机名
hash: null, //锚点
search: '?gws_rd=ssl', //查询字符串参数
query: 'gws_rd=ssl', //发送给服务器的数据
pathname: '/', //访问资源的路径名
path: '/?gws_rd=ssl', //路径
href: 'https://www.google.com/?gws_rd=ssl' }
【第二个参数实例演示】随便写的域名
> url.parse('https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1')
# 输出结果
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.zhangdanyang.com:80',
port: '80',
hostname: 'www.zhangdanyang.com',
hash: '#floor1',
search: '?from=zdy&search=hello',
query: 'from=zdy&search=hello',
pathname: '/article/list',
path: '/article/list?from=zdy&search=hello',
href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' }
> url.parse('https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1',true)
# 输出结果
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.zhangdanyang.com:80',
port: '80',
hostname: 'www.zhangdanyang.com',
hash: '#floor1',
search: '?from=zdy&search=hello',
query: { from: 'zdy', search: 'hello' },//第二个参数为true时,将query解析为对象
pathname: '/article/list',
path: '/article/list?from=zdy&search=hello',
href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' }
【第三个参数实例演示】
> url.parse('//www.google.com/',true)
# 输出结果
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '',
query: {},
pathname: '//www.google.com/',
path: '//www.google.com/',
href: '//www.google.com/' }
> url.parse('//www.google.com/',true,true)
# 输出结果
Url {
protocol: null,
slashes: true,
auth: null,
host: 'www.google.com',
port: null,
hostname: 'www.google.com',
hash: null,
search: '',
query: {},
pathname: '/',
path: '/',
href: '//www.google.com/' }
format
url.format(urlObject)
【实例】
> url.format({
... protocol: 'https:',
... slashes: true,
... auth: null,
... host: 'www.zhangdanyang.com:80',
... port: '80',
... hostname: 'www.zhangdanyang.com',
... hash: '#floor1',
... search: '?from=zdy&search=hello',
... query: 'from=zdy&search=hello',
... pathname: '/article/list',
... path: '/article/list?from=zdy&search=hello',
... href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' })
# 输出结果
'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1'
resolve
url.resolve(from, to)
【实例】
url.resolve('/one/two/three', 'four') // '/one/two/four'
url.resolve('http://example.com/', '/one') // 'http://example.com/one'
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'