node.js中的url.parse方法使用说明

*方法说明:*

讲一个URL字符串转换成对象并返回

代码如下:

url.parse(urlStr, [parseQueryString], [slashesDenoteHost])
 

接收参数:

urlStr                                       url字符串

parseQueryString                   为true时将使用查询模块分析查询字符串,默认为false

slashesDenoteHost               

默认为false,//foo/bar 形式的字符串将被解释成 { pathname: ‘//foo/bar' }

如果设置成true,//foo/bar 形式的字符串将被解释成  { host: ‘foo', pathname: ‘/bar' }

 

例子:

 

复制代码代码如下:
var url = require('url');
var a = url.parse('http://example.com:8080/one?a=index&t=article&m=default');
console.log(a);
 
//输出结果:

    protocol : 'http' ,     //底层使用的协议  (是http或者ftp等)
    auth : null ,    //是否有协议的双斜线
    host : 'example.com:8080' ,    //服务器的IP地址,或者说是域名
    port : '8080' ,    //端口号
    hostname : 'example.com' ,    //主机名
    hash : null ,    //哈希值,通常对应页面的锚点,就是指向页面的那个部分(#floor1这种)
    search : '?a=index&t=article&m=default',    //查询的字符串参数
    query : 'a=index&t=article&m=default',    //发送给http服务器的数据
    pathname : '/one',    //访问资源路径名
    path : '/one?a=index&t=article&m=default',    //路径
    href : 'http://example.com:8080/one?a=index&t=article&m=default'    //完整超链接
}
 
 
 
posted on 2015-11-09 10:59  Alone_Learner  阅读(309)  评论(0编辑  收藏  举报