可以看到url总共有5个function方法。
url.parse()
将一个url地址转换成一个url对象
输入命令行:url.parse(‘http://my.csdn.net/my/mycsdn‘)
url.format()
将一个解析后的URL对象、转成、一个格式化的URL字符串。
输入命令行:url.format({
protocol: ‘http:’,
slashes: true,
auth: null,
host: ‘www.imooc.com:8080’,
port: ‘8080’,
hostname: ‘www.imooc.com’,
hash: ‘#floor1’,
search: ‘?from=Allen&course=node’,
query: ‘from=Allen&course=node’,
pathname: ‘/video/6710’,
path: ‘/video/6710?from=Allen&course=node’,
href: ‘http://www.imooc.com:8080/video/6710?from=Allen&course=node#floor1’
})
url.resolve(from,to)
为URL或 href 插入 或 替换原有的标签。
querystring.stringify({
name:'Allen',course:['jade','node'],from:''
})
输出结果:
name:'Allen',course:['jade','node'],from:''
API中有三个参数:
> querystring.stringify({name:'Allen',course:['jade','node'],from:''})
'name=Allen&course=jade&course=node&from='
> querystring.stringify({name:'Allen',course:['jade','node'],from:''},',')
'name=Allen,course=jade,course=node,from='
> querystring.stringify({name:'Allen',course:['jade','node'],from:''},',',':')
'name:Allen,course:jade,course:node,from:'
2.querystring.parse()
反序列化
> querystring.parse('name=Allen&course=jade&course=node&from=')
{ name: 'Allen', course: [ 'jade', 'node' ], from: '' }
> querystring.parse('name=Allen,course=jade,course=node,from=',',')
{ name: 'Allen', course: [ 'jade', 'node' ], from: '' }
> querystring.parse('name:Allen,course:jade,course:node,from:',',',':')
{ name: 'Allen', course: [ 'jade', 'node' ], from: '' }
3.转义
querystring.escape()
> querystring.escape('你好世界')
'%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C'
4.反转义
querystring.unescape()
> querystring.unescape('%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C')
'你好世界'