url
node下打印url

引入url模块
var url = require('url')
parse方法
将url解析成对象,parse方法原型:
url.parse(urlStr[, parseQueryString][, slashesDenoteHost])
可传递三个参数,第一个必须
urlStr:要解析成对象的url字符串
parseQueryString:是否解析查询参数,默认为false
slashesDenoteHost:是否以斜线解析主机名,默认为false
-
只给第一个参数
image.png
-
第二个参数设为true
url中?之后的查询参数query解析成对象,可以和上面的图的query对比一下
image.png
-
第三个参数为true
也就是当不知道url协议时,以//为依据识别host
image.png
format方法
format就是parse的返过程,把对象转换成url字符串

resolve方法
var url = require('url');
2 var a = url.resolve('/one/two/three', 'four') ,
3 b = url.resolve('http://example.com/', '/one'),
4 c = url.resolve('http://example.com/one', '/two');
5 console.log(a +","+ b +","+ c);
6 //输出结果:
7 ///one/two/four
8 //http://example.com/one
9 //http://example.com/two
网友评论