nodejs里面的一个简单的模块,url模块。
url一共提供了三个方法,分别是url.parse(); url.format(); url.resolve()
1 url.parse(urlString,boolean,boolean)
parse这个方法可以将一个url的字符串解析并返回一个url的对象
参数:urlString指传入一个url地址的字符串
第二个参数(可省)传入一个布尔值,默认为false,为true时,返回的url对象中,query的属性为一个对象。
第三个参数(可省)传入一个布尔值,默认为false
1 url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash");
2 /*
3 返回值:
4 {
5 protocol: 'http:',
6 slashes: true,
7 auth: 'user:pass',
8 host: 'host.com:8080',
9 port: '8080',
10 hostname: 'host.com',
11 hash: '#hash',
12 search: '?query=string',
13 query: 'query=string',
14 pathname: '/p/a/t/h',
15 path: '/p/a/t/h?query=string',
16 href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
17 }
18 没有设置第二个参数为true时,query属性为一个字符串类型
19 */
1 url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash",true);
2 /*
3 返回值:
4 {
5 protocol: 'http:',
6 slashes: true,
7 auth: 'user:pass',
8 host: 'host.com:8080',
9 port: '8080',
10 hostname: 'host.com',
11 hash: '#hash',
12 search: '?query=string',
13 query: { query: 'string' },
14 pathname: '/p/a/t/h',
15 path: '/p/a/t/h?query=string',
16 href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
17 }
18 返回的url对象中,query属性为一个对象
19 */
2.url.format(urlObj)
format这个方法是将传入的url对象编程一个url字符串并返回
url.format({
protocol:"http:",
host:"182.163.0:60",
port:"60"
});
/*
返回值:
'http://182.163.0:60'
*/
3.url.resolve(from,to)
resolve(from, to)方法用于拼接URL,它根据相对URL拼接成新的URL
url.resolve("http://whitemu.com","gulu");
/*
返回值:
'http://whitemu.com/gulu'
*/