引入 url:
const url = require("url");
用于URL解析、处理等操作的解决方案
1.url.parse(urlStr[, parseQueryString][, slashesDenoteHost])
将url字符串转换成object对象
/**
* urlStr:需要处理的url字符串
* parseQueryString: 是否将查询参数也解析成对象
* 为true时将使用查询模块分析查询字符串,默认为false
* slashesDenoteHost: 解析主机处理,双斜线表示主机
* 默认为false,//foo/bar 形式的字符串将被解释成 { pathname: ‘//foo/bar‘ }
* 如果设置成true,//foo/bar 形式的字符串将被解释成 { host: ‘foo‘, pathname: ‘/bar‘ }
*/
实例1,仅指定url字符解析成对象
var url=require(‘url‘);
var url1=‘http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two‘;
var result=url.parse(url1);
{
protocol: ‘http:‘, //使用协议
slashes: true, //
auth: null, // 验证信息
host: ‘calc.gongjuji.net‘, //全小写的主机部分的URL,包括端口信息。
port: null, //端口
hostname: ‘calc.gongjuji.net‘,//小写的主机部分的主机
hash: ‘#one#two‘, //页面锚点参数部分
search: ‘?name=zhangsan&age=18‘,//查询参数部分,带?
query: ‘name=zhangsan&age=18‘, //查询参数部分
pathname: ‘/byte/‘, //目录部分
path: ‘/byte/?name=zhangsan&age=18‘,//目录+参数部分
href: ‘http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two‘ //最初解析的完整的网址。双方的协议和主机是小写。
}
实例2 ,指定是否解析参数部分成对象
//参数解析
var result2=url.parse(url1,true);
{ protocol: ‘http:‘,
slashes: true,
auth: null,
host: ‘calc.gongjuji.net‘,
port: null,
hostname: ‘calc.gongjuji.net‘,
hash: ‘#one#two‘,
search: ‘?name=zhangsan&age=18‘,
query: { name: ‘zhangsan‘, age: ‘18‘ }, //页面参数部分,已经解析成对象了
pathname: ‘/byte/‘,
path: ‘/byte/?name=zhangsan&age=18‘,
href: ‘http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two‘ }
实例3,主机特殊解析
//双斜线表示主机
var url2=‘//www.gongjuji.net/byte/?name=zhangsan#one‘;
{
protocol: null,
slashes: true,
auth: null,
host: ‘www.gongjuji.net‘,
port: null,
hostname: ‘www.gongjuji.net‘,
hash: ‘#one‘,
search: ‘?name=zhangsan‘,
query: { name: ‘zhangsan‘ },
pathname: ‘/byte/‘,
path: ‘/byte/?name=zhangsan‘,
href: ‘//www.gongjuji.net/byte/?name=zhangsan#one‘
}
var result3=url.parse(url2,true,true);
console.info(result3);
2.url.format(urlObj) 将json对象格式化成字符串
var url=require(‘url‘);
var obj1={
protocol: ‘http:‘,
slashes: true,
auth: null,
host: ‘calc.gongjuji.net‘,
port: null,
hostname: ‘calc.gongjuji.net‘,
hash: ‘#one#two‘,
search: ‘?name=zhangsan&age=18‘,
query: ‘name=zhangsan&age=18‘,
pathname: ‘/byte/‘,
path: ‘/byte/?name=zhangsan&age=18‘,
href: ‘http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two‘
};
var url1=url.format(obj1);
console.log(url1);//http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two
//请求参数为为json对象
var obj2={
protocol: ‘http:‘,
slashes: true,
auth: null,
host: ‘calc.gongjuji.net‘,
port: null,
hostname: ‘calc.gongjuji.net‘,
hash: ‘#one#two‘,
search: ‘?name=zhangsan&age=18‘,
query: { name: ‘zhangsan‘, age: ‘18‘ }, //页面参数部分,已经解析成对象了
pathname: ‘/byte/‘,
path: ‘/byte/?name=zhangsan&age=18‘,
href: ‘http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two‘
};
var url2=url.format(obj2);
console.log(url2); //http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two
//缺少参数的情况
var obj3={
protocol: null,
slashes: true,
auth: null,
host: ‘www.gongjuji.net‘,
port: null,
hostname: ‘www.gongjuji.net‘,
hash: ‘#one‘,
search: ‘?name=zhangsan‘,
query: { name: ‘zhangsan‘ },
pathname: ‘/byte/‘,
path: ‘/byte/?name=zhangsan‘,
href: ‘//www.gongjuji.net/byte/?name=zhangsan#one‘ };
var url3=url.format(obj3);
console.log(url3);//www.gongjuji.net/byte/?name=zhangsan#one
3.url.resolve(from, to)
返回从根目录指定到当前目录的绝对路径url
1.返回结果去除了参数和锚点
2.返回结果标准url路径格式
var url=require(‘url‘);
//指定相对路径
var url1=url.resolve(‘http://www.gongjuji.net/one/two/three‘,‘four‘);
console.log(url1); //http://www.gongjuji.net/one/two/four
//指定根目录的相对路径
var url3=url.resolve(‘http://www.gongjuji.net/one/two/three‘,‘/four‘);
console.log(url3); //http://www.gongjuji.net/four
//带参数的相对路径
var url2=url.resolve(‘http://www.gongjuji.net/one/two/three?name=zhangsan‘,‘four‘);
console.log(url2); //http://www.gongjuji.net/one/two/four
//非标准分隔符的原路径
var url4=url.resolve(‘http://www.gongjuji.net\\one#name1‘,‘/four‘);
console.log(url4);//http://www.gongjuji.net/four
//非标准分隔符的相对路径
var url5=url.resolve(‘http://www.gongjuji.net/one‘,‘\\two\\three‘);
console.log(url5);//http://www.gongjuji.net/two/three