const url = require('url');
----方法
--url.parse(urlStr, boolean1, boolean2)
解析一个url地址 返回一个url对象
urlStr url地址字符串
boolean1 默认false 当为true的时候返回的url对象中query属性返回的是一个对象
boolean2 默认false 当为true的时候 则//之后至下一个/之前的字符串会解析作为host
例如:let parseUrlObj = url.parse('http://www.baidu.com?name=test', true); console.log(parseUrlObj);
返回的url对象:
{
protocol: 'http', 客户段发出请求时使用的协议
slashes: true, 在协议与路径之间是否使用了//分隔符
auth: null, URL字符串中的认证部分
host: 'www.baidu.com', URL字符串中的完整地址和端口号
port: null, URL字符串中的端口号
hostname: 'www.baidu.com', URL字符串中的完整地址
hash: null, 散列字符串
search: '?name=test', 查询字符串 包含?
query: [Object: null prototype] { name: 'test' }, 查询字符串 不包含起始?
pathname: '/', URL字符串的路径 不包含查询字符串
path: '/?name=node.js', 路径 包含查询字符串
href: 'http://www.baidu.com/?name=test' 被转换的原URL字符串
}
--url.format(urlObj)
接收一个url对象 返回一个url字符串
--url.resolve(from, to)
拼接字符串网址
from 拼接时相对的基本url
to 要拼接的另一个url
例如:let urlStr = url.resolve('http://www.baidu.com', 'image');
console.log(urlStr); // http://www.baidu.com/image