url
模块用于处理与解析 URL
将一个url解析成对象 使用 url.parse(path,true).query
let searchhref = 'https://search.jin10.com/?keyword=gold&page=1&type=all&order=0' let hrefobj = url.parse(searchhref) console.log(hrefobj) // 输出结果如下: Url { protocol: 'https:', slashes: true, auth: null, host: 'search.jin10.com', port: null, hostname: 'search.jin10.com', hash: null, search: '?keyword=gold&page=1&type=all&order=0', query: 'keyword=gold&page=1&type=all&order=0', pathname: '/', path: '/?keyword=gold&page=1&type=all&order=0', href:'https://search.jin10.com/?keyword=gold&page=1&type=all&order=0' } let queryobj = url.parse(searchhref,true).query console.log(queryobj)
// 输出结果如下
{ keyword: 'gold', page: '1', type: 'all', order: '0' }
将一个对象拼装成一个url字符串路径 使用 url.format(onject)
let obj = { protocol: 'https', // 传输协议 hostname:'www.jinshi.com', // 域名 pathname : 'api/goods/list', // 请求路径 query :{ page : 1, count : 10, type: 'gold' } }
let resault = url.format(obj); console.log(resault) // 结果如下 https://www.jinshi.com/api/goods/list?page=1&count=10&type=gold