node.js在windows下的学习笔记(10)---URL模块

1.parse函数的作用是解析url,返回一个json格式的数组

url.parse('http://www.zjut.edu.cn');
{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.zjut.edu.cn',
  port: null,
  hostname: 'www.zjut.edu.cn',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: '/',
  href: 'http://www.zjut.edu.cn/' }
> 

2.parse函数的第二个参数是布尔类型,当参数为true时,会将查询条件也解析成json格式的对象。

url.parse('http://www.baidu.com?page=1',true);
{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?page=1',
  query: { page: '1' },
  pathname: '/',
  path: '/?page=1',
  href: 'http://www.baidu.com/?page=1' }
> 

3.parse函数的第三个参数也是布尔类型的,当参数为true,解析时会将url的"//"和第一个"/"之间的部分解析为主机名

url.parse('http://www.baidu.com/news',false,true);
{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/news',
  path: '/news',
  href: 'http://www.baidu.com/news' }
> 

4.format函数的作用与parse相反,它的参数是一个JSON对象,返回一个组装好的url地址

url.format({
protocol: 'http:',
hostname:'www.baidu.com',
port:'80',
pathname :'/news',
query:{page:1}
});
'http://www.baidu.com:80/news?page=1'

5.resolve函数的参数是两个路径,第一个路径是开始的路径或者说当前路径,第二个则是想要去往的路径,返回值是一个组装好的url

url.resolve('http://example.com/', '/one')
'http://example.com/one'

 

posted on 2014-09-16 15:41  michaeljunlove  阅读(300)  评论(0编辑  收藏  举报

导航