// 学习url模块
// url.parse()
// == 作用:把字符串形式的url网络路径解析成对象
// == 语法:url.parse(urlString,[parseQueryString],[,slashesDenoteHost])
// urlString:表示url网络地址,是一个字符串数据
// parseQueryString:是否解析查询字符串,默认是false
// slashesDenoteHost:是否拿到url //后面和第一个/前面的主机名(完整的地址 true和false没有区别)
// url.format()
// 作用:把url对象合并成一个完整的url
// 语法:url.format({url对象})
// 返回值:url字符串
// rel.resolve()
// 作用:把两端的url片段组合成一个完整的url
// 返回值:是一个完整的url字符串
const url = require('url')
let str = 'https://www.10086.cn/index/gd/index_200_768.html'
// console.log(url.parse(str))
// console.log(url.parse(str, true))
// console.log(url.parse(str, true, true))
/*
Url {
protocol: 'https:', //传输协议
slashes: true, // 是否有协议后面的双斜线 (//)
auth: null, // 指定认证 URL
host: 'www.10086.cn', // 主机(域名)
port: null, // 端口号
hostname: 'www.10086.cn', // 主机名
hash: null, // 哈希值
search: null, // ? 查询字符串
query: null, // 查询字符串
pathname: '/index/gd/index_200_768.html', // 请求路径
path: '/index/gd/index_200_768.html', // 请求路径+查询字符串
href: 'https://www.10086.cn/index/gd/index_200_768.html' // 获取的完整的前端传给我的url
}
*/
console.log(url.format({
protocol: 'https:', //传输协议
slashes: true, // 是否有协议后面的双斜线 (//)
auth: null, // 指定认证 URL
host: 'www.10086.cn', // 主机(域名)
port: null, // 端口号
hostname: 'www.10086.cn', // 主机名
hash: null, // 哈希值
search: null, // ? 查询字符串
query: null, // 查询字符串
pathname: '/index/gd/index_200_768.html', // 请求路径
path: '/index/gd/index_200_768.html', // 请求路径+查询字符串
href: 'https://www.10086.cn/index/gd/index_200_768.html' // 获取的完整的前端传给我的url
}))
// resolve
console.log(url.resolve('https://www.baidu.com', 'api/a=1'))
07-20
663
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交