url.parse(str,true) 返回 对象
url.format(obj) 返回字符
let url = require('url')
let str = "http://www.baidu.com:80/app/html/index.html?a=1&b=2#title"
// 把url字符串转成url对象
let obj = url.parse(str)
let query = obj.query
let queryArr = query.split('&')
let json = {}
for (let i = 0; i < queryArr.length; i++) {
let arr = queryArr[i].split('=')
json[arr[0]] = arr[1]
}
console.log(json);//{ a: '1', b: '2' }
let obj = url.parse(str,true)
// url.format(obj) ===> 对象转字符串
console.log(obj.query.a,obj.query.b);//1 2
console.log(url.format(obj));//http://www.baidu.com:80/app/html/index.html?a=1&b=2#title