nodejs中url模块。

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'
*/

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值